当前位置:首页 > 操作系统 > MacOs

javascript-如何覆盖Mac OS的快捷方式?

我正在开发一个Web应用程序,用于侦听按键事件的组合,例如CTRLB.
我的问题是在Mac上监听CTRL ArrowKey.这在PC上可以正常工作,但在Mac上,这是在台式机之间切换的快捷方式,因此不会触发第二个按键事件(箭头键).
有什么方法可以覆盖Mac OS的CTRL箭头快捷方式,或在Mac上的JavaScript中侦听此组合吗?

<code>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);
</code>

解决方法:

我认为这可能会产生想法.

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

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

})
</code>

【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!

相关教程推荐

其他课程推荐