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

  • 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
Get the latest in JavaScript, once a week, for free.