Skip to content

Instantly share code, notes, and snippets.

@ghoullier
Last active August 29, 2015 14:12
Show Gist options
  • Save ghoullier/99f65522baab0e92fe7e to your computer and use it in GitHub Desktop.
Save ghoullier/99f65522baab0e92fe7e to your computer and use it in GitHub Desktop.
Konami code detection
;(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