Vue monitors the keyboard, just bind it with @, and Vue provides aliases for several commonly used keys, so you don’t need to query the keyCode
all Key alias
.enter
# .space .up .down .left .right1. The input tag binds the esc key
and binds the event in <input type="text" @keyup.esc="KeyUpEsc">
KeyUpEsc:function(){ alert("监听到esc键") }
Binding event in
<el-input v-model="input" placeholder="请输入内容" @keyup.delete.native="KeyUpDelete"></el-input>
KeyUpDelete :function(){ alert("监听到delete键") },
created: function() { var _this = this; document.onkeydown = function(e) { let key = window.event.keyCode; if (key == 13) { _this.submit(); } }; },
methods: { submit: function() { alert("监听到enter键"); }, }
##For more js related knowledge, you can click me:
js tutorial
The above is the detailed content of vue.js listens to keyboard events. For more information, please follow other related articles on the PHP Chinese website!