我正在开发一个网络应用程序,它可以侦听键控事件的组合,例如 CTRL + B。我的问题是在Mac上侦听CTRL +箭头键。这在 PC 上工作正常,但在 Mac 上,这是在桌面之间切换的快捷方式,因此不会触发第二个键向下事件(箭头键(。有没有办法覆盖mac os CTRL + Arrow快捷方式,或者在mac上的javascript中侦听这种组合?
document.onkeydown = listenForSecondKey;
function listenForSecondKey(event){
console.log(event.key);
event.preventDefault ? event.preventDefault() : (event.returnValue=false);
if ((holdDown1 == true)&&(holdDown2 == true)){
if (event.which == push){
document.removeEventListener("keydown", keyGoingDown);
if (postcondition){
showPostCondition();
}
else{
killTable();
correctAnswerSubmitted();
}
}
else{
killTable();
incorrectAnswerSubmitted();
}
holdDown1 = false;
holdDown2 = false;
}
}
function keyGoingDown(event){
event.preventDefault ? event.preventDefault() : (event.returnValue=false);
if (event.key == hold1) {
holdDown1 = true;
}
else if (event.key == hold2){
if (holdDown1 == true){
holdDown2 = true;
}
}
else{
//Wrong, but also shouldn't detect push down
}
}
document.addEventListener("keydown", keyGoingDown);
我认为这可能会给出一个想法。
document.addEventListener('keydown', (e) => {
if ( e.key === 'ArrowLeft' ) {
e.preventDefault() // Stop other operations
}
})