Just for fun — Add Konami Command (secret code) for your site.

Jony Cheung
Jony’s blog
Published in
3 min readMar 25, 2011

Wrote something extremely stupid — a Konami secret code handler. You can try it on my site. http://www.jonycmusic.com/ (Up-Up-Down-Down-Left-Right-Left-Right-B-A)

Code source: http://jsfiddle.net/jonyc/8XSaS/4/

/*
*
* AUTHOR: Jonathan Cheung
* KonamiCodeTrigger.js
*
*/
var KonamiCodeTrigger = (function() {
// Saves the returned API
var self;
// Key History
var keyCodeHistory = [];
// All the triggers are stored in an Array;
var __triggers = [];
// Constants
var Key = {
UP: 38,
DOWN: 40,
LEFT: 37,
RIGHT: 39,
ESCAPE: 27,
a: 97,
b: 98,
A: 65,
B: 66
};
// Here we store the winning Combinations..
// The default is Up-Up-Down-Down-Left-Right-Left-Right-B-A / Up-Up-Down-Down-Left-Right-Left-Right-b-a
var winningCombos = [
String("" + Key.UP + Key.UP + Key.DOWN + Key.DOWN + Key.LEFT + Key.RIGHT + Key.LEFT + Key.RIGHT + Key.b + Key.a),
String("" + Key.UP + Key.UP + Key.DOWN + Key.DOWN + Key.LEFT + Key.RIGHT + Key.LEFT + Key.RIGHT + Key.B + Key.A)
];
//Let's save the original
var originalKeyPress;
if (document && document.onkeypress) {
originalKeyPress = document.onkeypress;
}
document.onkeypress = function(evt) {
var keyCode = Number(evt.keyCode);
var charCode = Number(evt.charCode);

//Process keyCode
switch (keyCode) {
case 0:
break;
case Key.ESCAPE:
//Reset the history if ESC is pressed
keyCodeHistory = [];
window.scrollTo(0, 0);
break;
default:
keyCodeHistory.push(Number(evt.keyCode));
break;
}
//Process charCode
switch (charCode) {
case 0:
break;
default:
keyCodeHistory.push(charCode);
// Here's where we find out if the Konami code existed
if (winningCombos.indexOf(keyCodeHistory.join("")) != -1) {
__triggers.forEach(function(item, index) {
(typeof(item) == 'function') && item();
});
}
break;
}
//Call our original keypress if it was there
originalKeyPress && originalKeyPress(arguments);
};

return self={
/*
* addTrigger()
* Adds a function to the callback stack.
* @callback {function} - The callback function you want to add
*/
addTrigger: function(callback) {
__triggers.push(callback);
},
/*
* removeTrigger()
* Removes a function from the callback stack.
* @callback {function} - The callback function you want to remove
*/
removeTrigger: function(callback) {
__triggers = __triggers.filter(function(item, index) {
return item != callback;
});
},
/*
* clearCombo()
* Clears the combo being used.
* @no prarameteres
*/
clearCombo: function() {
winningCombos = [];
},
/*
* addCombo()
* Adds a combo into the trigger. We can have multiple key combo to trigger the callback functions.
* @combo {string} - The string you want to use e.g. KonamiCodeTrigger.Key.A + KonamiCodeTrigger.Key.B
*/
addCombo: function(combo) {
winningCombos.push(String("" + combo));
},
/*
* removeCombo()
* Removes a combo.
* @combo {string} - The string you want to remove e.g. KonamiCodeTrigger.Key.A + KonamiCodeTrigger.Key.B
*/
removeCombo: function(combo) {
winningCombos = winningCombos.filter(function(item, index) {
return item != String("" + combo);
});
},
/*
* Key {}
* These are key constants.. You can add more to the Key costants above. Right now, only UP, DOWN, LEFT,RIGHT,A,B,a,b are in the constants as they are in the *original* Konami combo.
*/
Key:Key
}
})();
You can add more callback functions like this:/* Put this guy in your onload */
KonamiCodeTrigger.addTrigger(function() {
alert("BINGO! Now we do something interesting!");
});
And you can clear the original combo and add your own combo this way. (You need to assign the 'Key' object has the keyboard key mappings you want.)//KonamiCodeTrigger.clearCombo();
KonamiCodeTrigger.addCombo("" + KonamiCodeTrigger.Key.A + KonamiCodeTrigger.Key.B);

--

--

Jony Cheung
Jony’s blog

Senior Engineering Manager at Postman | ex Atlassian, Sony Playstation, Prosper Marketplace