我有一个需要隐藏/显示的元素,具体取决于用户是否按住以下任何键:
⌘
(也称为“元”)键但是,我无法捕获按键事件。
这是我当前的实现方式:
window.addEventListener('keydown', e => {
const { keyCode, metaKey, shiftKey } = e
const onlyMeta = ((keyCode === 91 || keyCode === 93) && metaKey)
const onlyShift = (keyCode === 16 && shiftKey)
if (onlyMeta) store.state.holdingMeta = true
if (onlyShift) store.state.holdingShift = true
})
window.addEventListener('keyup', e => {
const { keyCode, metaKey, shiftKey } = e
const onlyMeta = ((keyCode === 91 || keyCode === 93) && metaKey)
const onlyShift = (keyCode === 16 && shiftKey)
if (onlyMeta) store.state.holdingMeta = false
if (onlyShift) store.state.holdingShift = false
})
{p}的键不能识别键。