示例摘录:
document.addEventListener('keydown', (event) => {
if (event.code === 'Delete') {
console.log('The delete key was pressed' +
(event.ctrlKey ? ' while the control key was held down.' : '.'));
}
});
在此示例中,似乎永远不会应用第二个代码段。如果我将event.ctrlKey
更改为event.altKey
,然后按alt
+ del
,则会按照我的预期正确记录。
有人可以帮助我理解这种不一致的行为,并帮我找到解决办法吗?
编辑:我应该提到的东西 - 它似乎在浏览器(Chrome)中正常工作。应用场景实际上是基于电子的应用程序。我也注意到它并没有记录第二个片段 - keyEvent根本没有触发。现在这让我觉得它是一个冲突的键绑定,或者应用程序正在阻止密钥。我仍然想知道是否有(正确的)解决方法。答案 0 :(得分:0)
你可能在Mac上?比|| event.metaKey
更有帮助
document.addEventListener('keydown', (event) => {
if (event.code === 'Delete') {
console.log('The delete key was pressed' +
((event.ctrlKey || event.metaKey) ? ' while the control key was held down.' : '.'));
}
});