JavaScript
Article

jQuery Konami Code Listener

By Sam Deering

jQuery code snippet that listens for entry of a Japanese Konami code and then performs your desired action.

if ( window.addEventListener ) {
    var kkeys = [], konami = "38,38,40,40,37,39,37,39,66,65";
    window.addEventListener("keydown", function(e){
        kkeys.push( e.keyCode );
        if ( kkeys.toString().indexOf( konami ) >= 0 )
            // User entered Konami Code, do something cool!!!
            kkeys = [];
    }, true);
}

More info on Konami plugins here: http://plugins.jquery.com/project/konamiCode

Free Guide:

7 Habits of Successful CTOs

"What makes a great CTO?" Engineering skills? Business savvy? An innate tendency to channel a mythical creature (ahem, unicorn)? All of the above? Discover the top traits of the most successful CTOs in this free guide.

  • Pingback: jQuery Konami Code Listener « Big Engine Media()

  • http://www.barlisto.com Wouter

    Thanks for your code! Using it for my Barlisto web site :-)

    There’s a tiny mistake though: after you pressed the code, EACH key press will still satisfy your IF condition and thus execute your easter egg again. You can fix by resetting that kkeys array:

    if ( kkeys.toString().indexOf( konami ) >= 0 ){
    // User entered Konami Code, do something cool!!
    kkeys = [];
    }

    • jquery4u

      @Wouter, thanks mate, i’ll update the code.
      Sam

Recommended
Sponsors
Because We Like You
Free Ebooks!

Grab SitePoint's top 10 web dev and design ebooks, completely free!

Get the latest in JavaScript, once a week, for free.