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

前端之家收集整理的这篇文章主要介绍了javascript-如何覆盖Mac OS的快捷方式? 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

})
原文链接:https://www.f2er.com/js/531148.html

猜你在找的JavaScript相关文章