Skip to content
Advertisement

Detect keypress combination series with Javascript

For the Easter holiday, I’m wanting to have a little surprise “Easter Egg Hunt” on a site I develop for. Two of these five Easter Eggs I’m hiding will be keypress ordained. This won’t be like a “Press CTRL and TAB at the same time” type deal but will be a “Pres UP three times and then RIGHT three times” type of thing. This will be looking for a series of keypresses instead of just two pressed at once. I’ve got this function set up, but for some odd reason it isn’t working like it should.

NOTE: The script below is looking for the following keypress series:
surprise1 – LEFT (x3), RIGHT (x3), UP (x3), DOWN (x3)
surprise2 – SHIFT (x3), TAB (x3), CTRL (x3)

$(document.body).keydown(function(e) {
            surprise1(e);
            surprise2(e);
});

function surprise1(e) {
    var ev = (e) ? e : window.event;
    var k = ev.keyCode;
    if (k > 36 && k < 41) {
        typekeys[k] = isNaN(typekeys[k]) ? 0 : typekeys[k];
        typekeys[k]++;
        if (typekeys[37] == 3) {
            if (typekeys[37] == 3 && typekeys[39] == 3) {
                if (typekeys[37] == 3 && typekeys[39] == 3 && typekeys[38] == 3) {
                    if (typekeys[37] == 3 && typekeys[39] == 3 && typekeys[38] == 3 && typekeys[40] == 3) {
                        alert("You've found Surprise 1! Contact the site admin ASAP to get your prize!");
                        typekeys[37] = typekeys[39] = typekeys[38] = typekeys[40] = 0;
                    }
                } else {
                    typekeys[40] = 0;
                }
            } else {
                typekeys[38] = typekeys[40] = 0;
            }
        } else {
            if (typekeys[37] > 3) {
                typekeys[37] = 0;
            }
            typekeys[39] = typekeys[38] = typekeys[40] = 0;
        }
    } else {
        typekeys[37] = typekeys[39] = typekeys[38] = typekeys[40] = 0;
    }
};

function surprise2(e) {
    var ev = (e) ? e : window.event;
    var k = ev.keyCode;
    if (k > 8 && k < 18) {
        typekeys[k] = isNaN(typekeys[k]) ? 0 : typekeys[k];
        typekeys[k]++;
        if (typekeys[16] == 3) {
            if (typekeys[9] == 3) {
                if (typekeys[16] == 3 && typekeys[9] == 3 && typekeys[17] == 3) {
                    alert("You've found Surprise 2! Contact the site admin ASAP to get your prize!");
                    typekeys[16] = typekeys[9] = typekeys[17] = 0;
                }
            }
        } else {
            if (typekeys[16] > 3) {
                typekeys[16] = 0;
            }
            typekeys[9] = typekeys[17] = 0;
        }
    } else {
        typekeys[16] = typekeys[9] = typekeys[17] = 0;
    }
};

Mind telling me exactly as to why that isn’t working? It seems like to me it should work.

Advertisement

Answer

Try this: I’m using https://github.com/madrobby/keymaster jquery plugin

$(function () {
   var combination = ''
   key('left', function(){ 
       combination = 'left';
       checkCombination();
   });
   key('right', function(){ 
       combination+= 'right';
       checkCombination();
   });
   key('up', function(){ 
       combination+= 'up';
       checkCombination();
   });
   key('down', function(){ 
       combination+= 'down';
       checkCombination();
   });

   function checkCombination() {
      if(combination === 'leftrightupdown') {
        alert('surprise 1');  
      } 
   }
});​

Demo: http://jsfiddle.net/codef0rmer/BSdCq/

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement