Cyril F
Cyril F

Reputation: 1882

Combine keypress and keyup - jQuery

I'm trying to develop a jQuery plugin to do an action when the user enter a specific keyphrase.

For example, I want to match "HELLO" on keyup.

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

Then, I compare my controllerKey with my string "HELLO" (thanks to str.charCodeAt()) and some others things but this isn't important here. Everything works fine at this point.

My problem happens when I want to match "HeLLo" (in fact when the string had some uppercase). I saw on forums that keyup or keydown don't make any difference.

So I use keypress which manage it very well but keypress doesn't allow me to match arrow keys and so one (in Chrome).

I want to know if it's possible to combine keypress and keyup (only when keypress doesn't match the event).

Thanks in advance.

Upvotes: 8

Views: 19814

Answers (3)

mehmet
mehmet

Reputation: 685

You can use oninput event instance keyup keydown:


JQuery:

$(input).input(function(){ ...

javascript:

input.addEventListener("input", function (event) { ...

Upvotes: 0

Tooraj Jam
Tooraj Jam

Reputation: 1612

You can use a hidden input and key events in order to have cross browser case sensitive compare.

Upvotes: 2

lucuma
lucuma

Reputation: 18339

You can combine them like this:

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

   } else {
      // it is keypress
   }
});

Upvotes: 15

Related Questions