About

Konami.js is a JavaScript object that allows web developers to implement the Konami code on their webpages. By pressing up, up, down, down, left, right, left, right, B, A, Enter, one or more custom JavaScript functions will execute.

Methods

addEvent(function e)

Queues a function to be executed when the Konami code is entered by the user.

Returns an integer ID of the event, which can be used with removeEvent to disable the associated function from executing in future uses of the Konami code.

Parameters

function e - the function to be executed when the Konami code is entered by the user.

removeEvent(int id)

The function associated with the ID will no longer execute when the Konami code is entered.

Returns the ID of the removed event.

Parameters

int id - the ID of the event to remove.

Use

To use this script, add the following script to the end of your document, just before </body>:

<script src="//www.charlesstover.com/konami.js" type="text/javascript"></script>

Attach a function to execute when the Konami code is pressed by calling konami.addEvent:

window.addEventListener(
	"load",
	function() {
		konami.addEvent(
			function(id) {
				konami.removeEvent(id);
				document.getElementsByTagName("main").item(0).setAttribute("id", "konami");
			}
		);
	}
);

In this example, addEvent is called after the window has loaded to insure that konami.js has also loaded. Executing addEvent after the konami.js script tag can alleviate the need for this. When the Konami code is entered, the function is removed from the queue (so that it doesn't trigger the next time the user enters the Konami code on the page), and then the first <main> element's ID is changed to konami.

You can see what this does by entering the Konami code on this page! The konami ID triggers a CSS3 animation that causes the <main> element to animate.

Open Source

// konami.js (C) 2016-2018 Charles Stover
// www.charlesstover.com

// up, up, down, down, left, right, left, right, B, A, Start
var konami = {

		// Add a function to the queue to execute
		// once the Konami code has been entered.
		addEvent: function(f) {
			this.events.push(f);
			return this.events.length - 1;
		},

		// Check to see if the user's string of keys matches the Konami code.
		check: function() {

			// If they haven't entered anything, then it's not correct.
			if (!this.entered.length)
				return false;

			// For each character in the Konami code,
			for (var x = 0; x < this.code.length; x++) {

				// If they haven't even entered this many characters,
				// then they can't have entered the full code.
				if (!(x in this.entered))
					return false;

				/*
				If this character in the Konami code does not match,
					remove all entries up to this point.
				"up, up, down, down, left, up" becomes just "up"
					as the user attempts to start the code over.
				"up, up, down, down, left, D" becomes nothing
					as the user is no longer entering the Konami code.
				*/
				if (this.entered[x] != this.code[x]) {
					this.entered = this.entered[x] == this.code[0] ?
						[this.entered[x]] : [];
					return false;
				}
			}

			// If we haven't returned an error yet,
			// then the code was accurate in its entirety.
			// Reset and validate it.
			this.entered = [];
			return true;
		},

		// the Konami code itself
		code: [38, 38, 40, 40, 37, 39, 37, 39, 66, 65, 13],

		// the last keys entered by the user
		entered: [],

		// Queue of functions (set by addEvent) to execute
		// once the Konami code has been entered.
		events: [],

		// Bind the key logger once the window has loaded.
		init: function() {

			// Once the window has loaded,
			window.addEventListener(
				"keydown",
				function(e) {

					// Log the key press.
					konami.entered.push(e.keyCode);

					// If the last entered keys equate to the Konami code,
					if (konami.check()) {
						console.log("Konami Code Activated");

						// Execute each function in the queue.
						for (var x = 0; x < konami.events.length; x++) {
							if (typeof(konami.events[x]) == "function")
								konami.events[x](x);
						}
					}
				}
			);
		},

		// Remove a function from the queue.
		removeEvent: function(i) {
			this.events[i] = null;
			return i;
		}
	};

// Initiate the event bindings.
konami.init();