My JavaScript book is out! Don't miss the opportunity to upgrade your beginner or average dev skills.

Monday, August 17, 2009

Konami Code as DOM Event

If You do not know what is the Konami Code you are too young, or not geek at all.
In both cases, it does not matter, nobody is perfect, but what we should know is that some truly affectionate developer though to put this code to enable secret user powers, even if the the software is, as example, extremely recent.

So why not? Why can't we all have our secret mode in the most popular platform as the World Wide Web is?

Konami Code Event

var onkonamicode = function(el, fn){
// The Super Code Event by WebReflection - Mit Style
function onkonamicode(e){
konamicode.push((e || event).keyCode || e.charCode);
konamicode.length === 10 &&
konamicode.join(".") === ""
el.removeEventListener("keydown", onkonamicode, false);
el.detachEvent("onkeydown", onkonamicode);
i = setTimeout(clear, 1000);
function clear(){
konamicode = [];
var konamicode = [], i = 0;
el.addEventListener("keydown", onkonamicode, false);
el.attachEvent("onkeydown", onkonamicode);

How To Activate Konami Code

It is that simple:

onkonamicode(document, function(e){
alert("access guaranteed");

Now try to perform the magic sequence and that's it, you have extra power!


Alejandro Moreno said...

Did you add the code to this blog post?

Also, it would be nice if at the 10th character konamicode could be reset if it's not correct.

But I like it!

Andrea Giammarchi said...

Alejandro of course I added the code, why?

My implementation goes a bit farther than your suggestion, as soon as you stop typing it resets the array (the 1 second timeout)

In few words i you start and before 10 chars you type something wrong, you wait "a bit" and you can type again.

There is no need for a length based reset, also because the user cannot know if he is in the middle of that length or at the beginning so every moment is fine to type it ;)

Valentino Aluigi said...