jeffchannell.com

Konami Code

Posted in JavaScript
2009-06-25 22:51:06 +0000 UTC

There's a really cool site at konamicodesites.com that lists a whole bunch of sites that have easter eggs if you type a certain pattern while visiting. That pattern is the famous Konami Code. It lists some pretty large sites, like Facebook and Digg.

I went ahead and wrote a quick little bit of JavaScript that implements this functionality. It's loaded into the site you're looking at right now!

Here's the source code to make this work, released as Kopyleft (so do whatever you want with it) GNU/GPL, to begrudgingly comply with the rules of extensions.joomla.org.

var kcode = [38,38,40,40,37,39,37,39,66,65,13];
var kc_ptr = 0;
function konami( e )
{
	// make sure IE sees event
	e = e||window.event;
	// keyCode
	var kc = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
	// reset pointer if keycode isn't the next in sequence
	if( kc != kcode[kc_ptr] )
	{
		kc_ptr = 0;
		return;
	}
	// reset pointer & execute code
	if( ++kc_ptr == kcode.length )
	{
		kc_ptr = 0;
		// code to execute
		// replace the next line with the action you want to take
		console.log( 'konami' );
	}
}
window.addEvent( 'load', function( e )
{
		document.onkeydown = konami;
});