如何跟踪一个按钮然后单击另一个按钮的点击?

时间:2020-03-29 17:39:02

标签: javascript

我需要跟踪单击大写锁定,然后跟踪键盘快捷键ctrl + alt,即先单击大写锁定,然后单击键盘快捷键,这里是一个示例

if (e.code == "CapsLock") {
        if (e.ctrlKey && e.keyCode == 18) {
          alert();
        }
      }

但是此代码不起作用

1 个答案:

答案 0 :(得分:0)

我使用这样的函数:

document.addEventListener("keydown", function (e) {
    var caps = e.getModifierState && e.getModifierState('CapsLock');  // true if pressed, false if released
    document.onkeyup = function(event) {
    if(caps === true){
        /** Validate keys are to be processed */
        var keycode = event.which;
        // works off course only if ctrl key is kept pressed during key stroke = normal behavior in most OS
        // Should it work like a ctrl-Lock function you have to work with a state variable
            if ((event.ctrlKey === true) && (keycode === 18)) {  
                alert();
            }
            else{
                /** Let it happen, don't do anything */
                return;
            }
        }
    };
});
相关问题