JavaScript Konami Code

The Konami Code is a sequence of button presses that triggered a cheat of some description in many Konami games. On the title screen you'd press Up, Up, Down, Down, Left, Right, Left, Right, B and A on your controller and the cheat would be activated/revealed/whatever.

Many web folks like to include a hidden response to the Konami Code and it's not a terribly difficult thing to do, so here's how we go about it:

This simple piece of JavaScript calls a function called konamiGo(); whenever the correct sequence of keys is pressed. Here's what happens line by line:

  1. Define a function called konamiCheck() which takes note of an event.
  2. Get the value of the keypress that spawned the function.
  3. Add a string version of this value to a variable called konami.
  4. Compare the last 10 keys (2 bytes each) to our desired sequence, '38384040373937396665';
  5. If they match, spawn konamiGo();

The two lines after the function set the konami variable as a global and attach the konamiCheck() function to the onkeydown event so that it is called every time the user presses a key.

Why are we comparing against 38384040373937396665? Well that's the ascii codes for all of the keypresses in the Konami Code sequence - Up is 38, Down is 40 and so on. Assuming the user enters the code correctly they will match this value and spawn your surprise.

All that's left for you to do is write your own konamiGo(); function to do something interesting.

Bonus: Tweetable Version

For the minimalists amongst you, here's the Konami Code in 119 characters (once you remove the CrLf):

Leave a comment if you use this to add a surprise to your site.

 

bl0ke

boing

Subtitle Tools

A simple online tool to perform subtitle adjustments. This is a Work in Progress that I'll expand as and when I find stuff in my own collection that needs fixing.25th March 2014

C64 Screenshot Challenge

Test your knowledge of C64 game screens with this fun little game based on Catchphrase.15th September 2013

Atari 2600 Box Art Quiz

Test your knowledge of (or simply marvel at the irrelvence of) Atari Box Art in this spectacularly underwhelming quiz!2nd September 2013

JavaScript Konami Code

How to create an event that triggers when a user enters the Konami Code into your website.29th August 2013

Parallax Wallpaper Scroll

How to achieve that groovy vertical parallax scrolling effect with minimum effort.21st August 2013

Minecraft Redstone Lighthouse

A guide to creating a lighthouse effect in Minecraft using Redstone lamps and repeaters.29th August 2013

LazyTown 'You are a Pirate!' Lyrics

Lyrics and video from LazyTown's incredibly addictive 'You are a Pirate!' song.18th August 2013