Source: Konami.js

/**
 * Makes a new Konami code object
 *
 * @constructor
 */
function Konami() {
	// Always working reference to this
	var self = this;

	// Make this an event emitter if SoupEvents are available
	if (Konami.eventsAvailable)
		SoupEvents.makeEventEmitter(this);
	else
		console.warn("SoupEvents is not available. Konami objects will not be able to register event listeners or emit events. Please override the \"codeEntered\" property to provide a callback when the code is entered");

	// Keys entered
	var keys = [];

	// Checks if the code has been entered, and fires the function if it has
	function check() {
		// Number of keys pressed must equal length of code
		if (keys.length == self.code.length) {
			// Assume the code is entered
			var codeEntered = true;

			// Check if the code is actually entered
			// Iterate through the code
			for (var i = 0; i < self.code.length; i++) {
				// Check if the key is correct
				if ((Array.isArray(self.code[i]) && self.code[i].indexOf(keys[i]) < 0) || (!Array.isArray(self.code[i]) && self.code[i] != keys[i])) {
					codeEntered = false;
					break;
				}
			}

			// Is the code really entered?			
			if (codeEntered) {
				// Reset the keys entered
				keys = [];

				// Run the function
				self.codeEntered();

				// Dispatch the "codeentered" event
				dispatchEvent("codeentered");
			}
		}
	}

	// Listen for keys
	document.body.addEventListener("keydown", function(evt) {
		// Add the key to the key array
		keys.push(evt.key);

		// Dump old keys
		while (keys.length > self.code.length) {
			keys.shift();
		}

		// Check if the code has been entered
		check();
	});

	// Dispatch events if available
	function dispatchEvent(type, data) {
		if (typeof self.dispatchEvent == "function")
			self.dispatchEvent(new CustomEvent(type, {detail: data}));
	};

}

// Key constants
Object.defineProperties(Konami, {
	/**
	 * Constant for the "Up" arrow key
	 *
	 * @memberof Konami
	 */
	UP: {get: function() {return  ["Up", "ArrowUp"];}},
	/**
	 * Constant for the "Down" arrow key
	 *
	 * @memberof Konami
	 */
	DOWN: {get: function() {return  ["Down", "ArrowDown"];}},
	/**
	 * Constant for the "Left" arrow key
	 *
	 * @memberof Konami
	 */
	LEFT: {get: function() {return  ["Left", "ArrowLeft"];}},
	/**
	 * Constant for the "Right" arrow key
	 *
	 * @memberof Konami
	 */
	RIGHT: {get: function() {return  ["Right", "ArrowRight"];}},
	/**
	 * Constant for the "B" key, both capital and non-capital
	 *
	 * @memberof Konami
	 */
	B: {get: function() {return  ["b", "B"];}},
	/**
	 * Constant for the "A" key, both capital and non-capital
	 *
	 * @memberof Konami
	 */
	A: {get: function() {return  ["a", "A"]}},
	/**
	 * Checks if Konami objects are able to register event listeners and dispatch events
	 *
	 * @memberof Konami
	 */
	eventsAvailable: {get: function() {return typeof SoupEvents != "undefined" }}
});

/**
 * The code to enter. Defaults to the Konami code. Can be overridden by user
 */
Konami.prototype.code = [Konami.UP, Konami.UP, Konami.DOWN, Konami.DOWN, Konami.LEFT, Konami.RIGHT, Konami.LEFT, Konami.RIGHT, Konami.B, Konami.A];

/**
 * Function to call when {@link Konami#code} is entered. Should be overridden by user
 */
Konami.prototype.codeEntered = function() {
	console.log("Konami code entered. Override the \"codeEntered\" field of this object to set a function to run when the code is entered");
};