Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override Mac OS shortcuts?

Tags:

javascript

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);
like image 629
Jake Avatar asked Jan 24 '26 03:01

Jake


1 Answers

I think this might give an idea.

document.addEventListener('keydown', (e) => {

    if ( e.key === 'ArrowLeft' ) {
        e.preventDefault() // Stop other operations
    }

})
like image 73
Minan Avatar answered Jan 26 '26 18:01

Minan