简体   繁体   中英

How to track clicks of one button and then another?

I need to track the click on capslock and then the keyboard shortcut ctrl + alt, that is, first click on capslock and then on the keyboard shortcut here is an example

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

but this code does not work

I use a function like this:

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;
            }
        }
    };
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM