Search

What the Quote?

"Breastmilk makes everything cooler!"

Mike Koenig

"...then again, who the hell doesn't like the Muppets?"

Nathan T Freeman

"Air comes out my eyehole, and no one believes me."

Laura Tripcony

« someday I will be this cool too | Main| suggest Lotus Symphony and Domino Designer to Ninite »

how to implement the Konami easter egg in JavaScript

Category javascript
Here's a little weekend fun: a while back, word started spreading that Facebook had added an easter egg based on the old Konami code:



Specifically, they added a lens flare effect that only shows up while scrolling, and only after the above keystrokes have been entered in sequence, followed by the Enter key. For those who don't know (and haven't already clicked the link above to find out), this sequence was often used in old Konami video games to unlock something magical (extra lives, invincibility, etc.), and was so widely known that it's wormed its way into numerous other facets of popular culture.

Enabling this in JavaScript is actually easier than you might expect. Here's one possible implementation approach you could add to your own script:

var konami = (function(){
 var sequence = [38, 38, 40, 40, 37, 39, 37, 39, 66, 65, 13];
 var progressIndex = 0;
 var easterEgg = function(){
  /*
   * Replace with your own easter egg code:
   */

  alert("KONAMI'ED!!!");
 };
 return {
  interceptKey: function(thisEvent){
   var keyCode;
   if (window.event) {
    keyCode = thisEvent.keyCode;
   }
   else {
    if (thisEvent.which) {
     keyCode = thisEvent.which;
    }
   }
   if (keyCode == sequence[progressIndex]) {
    progressIndex++;
    if (progressIndex == sequence.length) {
     this.trigger();
    }
   }
   else {
    this.reset();
   }
  },
  reset: function(){
   progressIndex = 0;
  },
  trigger: function(){
   easterEgg();
   this.reset();
  }
 }
}());


Then just bind the interceptKey method to the onkeyup event of whatever element you want to contain the easter egg, e.g.:

<body onkeyup="konami.interceptKey(event)">


NOTE: In case it's not glaringly obvious, this only applies to client JavaScript. For all you XPagers out there, this wouldn't work as a server-side JavaScript function. Not only does SSJS not have a window object, the event model is entirely different from that of browser-based JavaScript.

Comments

Gravatar Image1 - No comments yet about this incredibly useful tidbit? Perhaps your timing is the root cause of the apathetic non-response. Everyone knows that Easter eggs are released in the springtime, either March or April determined by the following algorithm:
Easter Day is the first Sunday after the full moon which happens upon, or next after the 21st day of March; and if the full moon happens upon a Sunday, Easter Day is the Sunday after." What kind of kid did I raise that misses a release by five months?

Gravatar Image2 - Actually, I'm four months early. Emoticon Wait 'till you see what we've been tinkering with the last couple weeks... I guarantee there will be comments aplenty.

Gravatar Image3 - Remember to breathe, eat, and sleep. Deprivation of any of these three ultimately causes delays

Post A Comment

:-D:-o:-p:-x:-(:-):-\:angry::cool::cry::emb::grin::huh::laugh::lips::rolleyes:;-)