I'm developing a web app which listens for combinations of keydown events, e.g. CTRL + B. My problem is listening for CTRL + ArrowKey on mac. This works fine on PC, but on Mac this is a shortcut to switch between desktops, so the second keydown event (arrow key) does not trigger. Is there any way to override the mac os CTRL+Arrow shortcut, or listen for this combination in javascript on mac?
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);
I think this might give an idea.
document.addEventListener('keydown', (e) => {
if ( e.key === 'ArrowLeft' ) {
e.preventDefault() // Stop other operations
}
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With