// 在頁面鉤子 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)
}
}
效果(自己試試)