Skip to content

Instantly share code, notes, and snippets.

@mtbdeano
Last active May 27, 2021 14:02
Show Gist options
  • Save mtbdeano/6ef1d273949fe114bd502adb32989cad to your computer and use it in GitHub Desktop.
Save mtbdeano/6ef1d273949fe114bd502adb32989cad to your computer and use it in GitHub Desktop.
a simple plain JS konami code listener
// 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