Last active
December 22, 2015 13:59
-
-
Save nhanb/6482897 to your computer and use it in GitHub Desktop.
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
/** | |
* Trigger something totally inappropriate when user enters the | |
* right sequence of the Konami code. | |
* | |
* callback is the function you want to execute when the complete sequence | |
* has been entered. | |
*/ | |
var konamiCode = function(callback) { | |
// The famous Konami sequence in keycodes | |
// up up down down left right left right b a | |
var sequence = [38, 38, 40, 40, 37, 39, 37, 39, 66, 65]; | |
var ko = sequence; | |
// Get the rest of this function's arguments (if any). | |
// These will be used as the callback's arguments. | |
var args = []; | |
for (var i = 1; i < arguments.length; i++) { | |
args.push(arguments[i]); | |
} | |
document.addEventListener('keydown', function(e) { | |
if (e.keyCode == ko[0]) { | |
ko.splice(0,1); | |
if (ko.length === 0) { | |
callback.apply(null, args); | |
} | |
} else { | |
ko = sequence; | |
} | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment