keydown:鼠标按下触发
keyup:鼠标按下再抬起触发
Vue中常用的按键别名有:
回车 => enter
删除 => delete (捕获“删除”和“退格”键)
退出 => esc
空格 => space
换行 => tab
上 => up
下 => down
左 => left
右 => right
<!DOCTYPE html>
<html lang="en"><head><script type="text/javascript" src="../js/vue.js"></script><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>键盘事件</title>
</head><body><div id="root"><input type="text" placeholder="按下回车提示输入" @keyup.enter="showInfo"></div><script>new Vue({el: "#root",data: {},methods: {showInfo(e) {alert(e.target.value)}},})</script>
</body></html>
Vue未提供别名的按键,可以使用按键原始的key值去绑定,但注意要转为kebab-case (短横线命名),例如:
<input type="text" placeholder="按CapsLock执行" @keyup.caps-lock="showInfo">
系统修饰键(用法特殊): ctr1、alt、shift、meta
(1).配合keyup使用: 按下修饰键的同时,再按下其他键,随后释放其他键,事件才被触发.
(2).配合keydown使用: 正常触发事件。
也可以使用keyCode去指定具体的按键 (不推荐)
<input type="text" placeholder="按下回车提示输入" @keyup.13="showInfo">
Vue.config.keyCodes.自定义键名 = 键码,可以去定制按键别名
<!DOCTYPE html>
<html lang="en"><head><script type="text/javascript" src="../js/vue.js"></script><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>键盘事件</title>
</head><body><div id="root"><input type="text" placeholder="按下回车提示输入" @keyup.huiche="showInfo"></div><script>Vue.config.keyCodes.huiche = 13new Vue({el: "#root",data: {},methods: {showInfo(e) {alert(e.target.value)}},})</script>
</body></html>