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 seires:
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.

Answers:

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/

Answer

Here is my solution. I had to do some funny stuff to compare arrays as described here. I'm sure you can fit the general gist of this script to fit your needs....

var seqs = [ [37,37,37,38,38,38,39,39,39,40,40,40], [9,9,9,16,16,16,17,17,17] ];
var seq  = [];

var messages=["You've found Surprise 1! Contact the site admin ASAP to get your prize!", "You've found Surprise 2! Contact the site admin ASAP to get your prize!"];

window.addEventListener("keydown", function(e){
    seq.push(e.keyCode);
    var eq = function(a,b){ return !( a<b || b<a ); };
    for ( var i = 0; i < seqs.length; i++ ) {
        if ( eq( seq, seqs[i].slice(0,seq.length) )) {
          if ( eq(seq, seqs[i]) ) {
            alert( messages[i] );
            seq = [];
          }
        return;
        }
    }
    seq = [];
});
Answer

Here's how I solved this one...

var nums = [38, 38, 40, 40, 37, 39, 37, 39, 66, 65];
var n = nums.slice();
$(document).keydown(function(e){
  if(e.which == n[0]){
    n.shift();
  } else n = nums.slice();
  if(n.length == 0) {
    //success!
    n = nums.slice();
  }
});

kudos if you know what the sequence is. ;)

Answer

this is better:

$(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();
});

key(!'down' && !'left' && !'right' && !'up',function() {
    combination = '';
});

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

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.