TIL: Capture keydown events on DIV elements

How to capture keydown/keypress events on HTML elements that aren't inputs

2023-09-22
frontend
vue
Share on

Just set the tabindex attribute, then you’ll be able to listen to keydown — or keyup, keypress — events like this:

<div tabindex="1" @keydown.esc="doSomething()">
  I can emit keydown events now!
</div>