vue监听按下ctrl或shift
// 在页面钩子 mounted() 处调用此函数,增加按键监听事件watchKeyEvent () {const setKeyStatus = (keyCode, status) => {switch (keyCode) {case 16:if (this.onShfit === status) returnconsole.log('shif', status ? '按下' : '抬起')
·
// 在页面钩子 mounted() 处调用此函数,增加按键监听事件
watchKeyEvent () {
const setKeyStatus = (keyCode, status) => {
switch (keyCode) {
case 16:
if (this.onShfit === status) return
console.log('shif', status ? '按下' : '抬起')
this.onShfit = status
break
case 17:
if (this.onCtrl === status) return
console.log('ctrl', status ? '按下' : '抬起')
this.onCtrl = status
break
}
}
document.onkeydown = (e) => {
setKeyStatus(e.keyCode, true)
}
document.onkeyup = (e) => {
setKeyStatus(e.keyCode, false)
}
}
效果(自己试试)
更多推荐
已为社区贡献3条内容
所有评论(0)