Created
April 15, 2015 01:32
-
-
Save shadowfacts/b7d3bfbfe3dc2a9c602c to your computer and use it in GitHub Desktop.
jQuery Konami code detector
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*! | |
* SimpleKonami | |
* Copyright: WTFPL | |
* Version: 1 | |
* Requires: jQuery v1.3.2 or later | |
*/ | |
//Set up our array of needed keys, and variables. | |
neededkeys = [38,38,40,40,37,39,37,39,66,65], started = false, count = 0; | |
$(document).keydown(function(e){ | |
key = e.keyCode; | |
//Set start to true only if having pressed the first key in the konami sequence. | |
if(!started){ | |
if(key == 38){ | |
started = true; | |
} | |
} | |
//If we've started, pay attention to key presses, looking for right sequence. | |
if(started){ | |
if(neededkeys[count] == key){ | |
//We're good so far. | |
count++; | |
} else { | |
//Oops, not the right sequence, lets restart from the top. | |
reset(); | |
} | |
if(count == 10){ | |
//We made it! Put code here to do what you want when successfully execute konami sequence | |
//Reset the conditions so that someone can do it all again. | |
reset(); | |
} | |
} else { | |
//Oops. | |
reset(); | |
} | |
}); | |
//Function used to reset us back to starting point. | |
function reset() { | |
started = false; count = 0; | |
return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment