Listening to "esc" key event on div component in Vue.js
For anyone who wanders here from google, in Vue 2...
<div @keydown.esc="something_in_your_methods"></div>
The secret for making keydown
events work on divs and other non-focusable elements is to add a tabindex
attribute:
<div tabindex="0"
@keydown.left="previousImage"
@keydown.right="nextImage" />
Now the div has become a focusable element and the key events will be triggered.
Here is more info on focusable elements and tabindex