Last active
August 29, 2015 14:12
-
-
Save ghoullier/99f65522baab0e92fe7e to your computer and use it in GitHub Desktop.
Konami code detection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;(function main(exports) { | |
var KONAMI_KEYS = [38, 38, 40, 40, 37, 39, 37, 39, 66, 65] | |
var KONAMI_KEYS_LENGTH = KONAMI_KEYS.length | |
var callbacks = [] | |
var index = 0 | |
document | |
.documentElement | |
.addEventListener('keydown', detect, true) | |
exports.registerCallback = registerCallback | |
function registerCallback(callback) { | |
callbacks.push(callback) | |
} | |
function invoke(callback) { | |
callback() | |
} | |
function detect(event) { | |
if (event.keyCode === KONAMI_KEYS[index++]) { | |
if (KONAMI_KEYS_LENGTH === index) { | |
index = 0 | |
callbacks.forEach(invoke) | |
} | |
} else { | |
index = 0 | |
} | |
} | |
}(this.konami = {})) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment