Keyboard shortcut - jQuery

I am trying to develop a jQuery plugin to perform an action when a user enters a specific passphrase.

For example, I want to match "HELLO" on the keyboard.

var controllerKey = [];
$(window).keyup(function(evt) {
    var code = evt.keyCode ? evt.keyCode : evt.which;
    controllerKey.push(code);
}
[...]

Then I compare my Key controller with my "HELLO" string (thanks to str.charCodeAt ()) and some other things, but that doesn't matter here. At this point, everything is working fine.

My problem occurs when I want to match "HeLLo" (actually, when the string was uppercase ). I saw on the forums that keyup or keydown do not make any difference.

So, I use keypress, which manages it very well, but keypress does not allow me to map arrow keys and therefore one (in Chrome).

I want to know if it is possible to combine keystrokes and keyup (only when keypress does not match the event).

Thanks in advance.

+5
source share
2 answers

You can combine them as follows:

$(window).on('keyup keypress', function(e) {
   if (e.type=="keyup") {

   } else {
      // it is keypress
   }
});
+9
source

You can use hidden input and key events to compare them with a cross browser.

+2
source

All Articles