最近需要触屏后才能监听键盘事件,怎么回事呢! (版本:3.4.18),以前都不需要。
代码如下:
mounted () {
const onKey = (event) => {
const keys1 = ['type', 'timeStamp']
const keys2 = ['altKey', 'code', 'ctrlKey', 'isComposing', 'key', 'location', 'metaKey', 'repeat', 'shiftKey']
const keys3 = ['char', 'charCode', 'keyCode', 'keyIdentifier', 'keyLocation', 'which']
const data = {}
keys1.concat(keys2, keys3).forEach(key => data[key] = event[key])
this.$ownerInstance.callMethod('onEvent', data)
}
const names = ['keydown', 'keyup']
names.forEach(name => {
document.addEventListener(name, onKey, false)
})
this.$on('hook:beforeDestroy', () => {
names.forEach(name => {
document.removeEventListener(name, onKey, false)
})
})
}
0 个回复