K
K
koliane2018-05-17 23:22:06
Vue.js
koliane, 2018-05-17 23:22:06

How to hang a keypress handler on document/window in Vue.js?

I'm using Vue.js
It is necessary that when a certain key (or key combination), such as Del or Backspase, is pressed anywhere on the page, a press handler is called.
How can this be done with Vue.js?
Events like @keydown work, as I understand it, only in input fields.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2019-06-21
@koliane

Use addEventListener when instantiating the component:

methods: {
  onKeyDown(e) {
    ...
  },
  ...
},
created() {
  document.addEventListener('keydown', this.onKeyDown);
},

https://jsfiddle.net/jqr576oa/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question