Last active
May 27, 2021 14:02
-
-
Save mtbdeano/6ef1d273949fe114bd502adb32989cad to your computer and use it in GitHub Desktop.
a simple plain JS konami code listener
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
// https://en.wikipedia.org/wiki/Konami_Code | |
// up, up, down, down, left, right, left, right, b, a, esc (in the real world was "start") | |
let konami_code = [38, 38, 40, 40, 37, 39, 37, 39, 66, 65, 27]; | |
let pressed = []; | |
document.addEventListener("keydown", event => { | |
// 229 is the key code android devices send when their software keyboards are guesing key presses | |
// https://bugs.chromium.org/p/chromium/issues/detail?id=118639 | |
if (event.isComposing || event.keyCode === 229) { | |
return; | |
} | |
// check the code | |
pressed.push(event.which); | |
if (pressed.length > konami_code.length) { | |
pressed.shift(); | |
} | |
is_secret = (konami_code.length == pressed.length) && | |
konami_code.reduce((accum, val, i, s) => { | |
return accum && (val == pressed[i]); | |
}, true); | |
if (is_secret) { | |
console.log('you pressed the secret code'); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment