Take the 2-minute tour ×

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;

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.

share|improve this question
Are they entering this phrase inside an input? Also you mention your problem is comparing strings of different cases and then you are asking about keypress and keyup? –  lucuma May 31 '12 at 20:26
No. It's a plugin to do like a "konami code" but with the password of your choice. I'm comparing the first string (key-phrase) with the user input –  Cyril F May 31 '12 at 20:28
I think I'm too old to know what konomi code is however I have provided you an answer about combining both the events.. –  lucuma May 31 '12 at 20:35
You can have a try here : konamicodesites.com Press the following keys : "Up / Up / Down / Down / Left / Right / Left / Right / B / A" –  Cyril F May 31 '12 at 20:38
Ok, I get it now, just didn't know the name.. Reminds me of commando from back in the day. +1 for teaching me that one! –  lucuma May 31 '12 at 20:41

2 Answers 2

up vote 6 down vote accepted

You can combine them like this:

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

   } else {
      // it is keypress
share|improve this answer
Consequently you can easily compare strings by s.toUpperCase() == t.toUpperCase() –  lucuma May 31 '12 at 20:36
Your answer will help me. Thanks. (I don't want to use upperCase for the comparaison because I want to keep this information) –  Cyril F May 31 '12 at 20:39

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

share|improve this answer

Your Answer


