简体   繁体   中英

Combination of Keypresses using JS / Jquery ( Escape & Shift +Escape )

Is there a way in js/jquery how to have these 2 combination of keypresses?

ESCape key and SHIFT + ESCape key

when I implemented it using

    document.onkeydown = function(e){if (e == null) {keycode = event.keyCode;}
    else         {keycode = e.which;}
    if(keycode == 27){closeAll();}}

    //upon pressing shift + esc
    $(document).bind('keypress',function(event)
    {
          if(event.which === 27 && event.shiftKey)
        {
      closetogether();
       }

    });

the escape button works perfectly but the one with the shift + esc is getting confused I think because its doing nothing. Dont worry the function works as when i change the combining key 27 to 90 (z) for example it works just fine.

Can someone opt me for a better way?

Why don't you bind the keydown event using jQuery? That way you would already have a normalized event variable. You can also check the status of the shift key in the same handler.

These events send different keycodes back. Use keyup/keydown for capturing certain keys by scancodes and use keypress to capture actual text input by characters.

$(document).bind('keydown', function(event) {
    if(event.which === 27){
        if(event.shiftKey){
            closetogether();
        } else {
            closeAll();
        }
    }
});

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