Konami Code in JavaScript

by Jason on June 9, 2012

Others have already done this many times, but I thought it would be an interesting challenge this morning to implement the Konami code in such a way that you can attach a callback which will be fired only after the code is successfully entered.

This generic approach is nice because it allows developers to do whatever they want when the code is entered.

Check it out
Github

2 comments

Hi,
Your Konami code implementation is unforgiving in that it assumes all keystrokes are part of the code entry attempt. That’s fine for many uses, but when I did a similar implementation a few years ago for a consumer application you’ve heard of and possibly used, I had to be more forgiving to allow for miscellaneous keystrokes before the intended sequence began. So, the application now tracks key history and compares the last sequence.length keystrokes against the sequence. It’s much more perf-intensive, but much more forgiving.

by Matt on August 12, 2012 at 9:37 pm. #

Hi Matt,

I don’t see how it’s unforgiving? It does not prevent another event from also picking up on the keystrokes, so it will not break any existing applications that require keyboard input.

And also, you have the ability to specify a target in the configuration. So, for instance, you could have it only listen for keyboard input when a particular input field or div is in focus.

by Jason on September 17, 2012 at 5:05 pm. #

Leave your comment

Required.

Required. Not published.

If you have one.