After some experimentation, I found that browsers do not allow canvas elements to be focused by default. Other active elements like buttons or forms respond to keyup events without any issues.
Fortunately, there is a property called tabindex
that determines if an element can be focused and in what order when tabbing through. Setting the tabindex
to 0 will make the canvas focused by default. The interaction between canvas elements and keyboard focus is not well-documented, but it appears that browsers tend to resist focusing on canvases compared to other element types.
Demonstrating this behavior can be tricky in a snippet, as it essentially asserts the tabindex
for the entire window. You may need to click on the blue area before pressing escape to observe the effect.
Vue.config.productionTip = false;
new Vue({
template: `<div>
<canvas tabindex="0" v-on:keyup.esc="abortThing" style="width:100px;height:100px;background-color:blue"></canvas>
</div>`,
methods: {
abortThing(event) {
console.log(event);
}
}
}).$mount("#app");
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app"></div>