Skip to content

Instantly share code, notes, and snippets.

@drwpow
Last active November 9, 2021 19:18
Show Gist options
  • Save drwpow/8e910dc3209afa1d120196f5c1aaf829 to your computer and use it in GitHub Desktop.
Save drwpow/8e910dc3209afa1d120196f5c1aaf829 to your computer and use it in GitHub Desktop.

👾 Konami Code (browser-only)

Tiny, lightweight, cross-browser compatible. Only 461 bytes minified!

Instructions

Copy & paste into a <script> tag, and modify line #16 with your own custom action!

const KEY_DELAY = 2000; // Maximum time to allow in between key presses (Default: 2 seconds)
const KONAMI_CODE = ['ArrowUp', 'ArrowUp', 'ArrowDown', 'ArrowDown', 'ArrowLeft', 'ArrowRight', 'ArrowLeft', 'ArrowRight', 'KeyB', 'KeyA'];
let keyBuffer = [];
let bufferTimeout;
window.addEventListener('keyup', (evt) => {
clearTimeout(bufferTimeout);
bufferTimeout = setTimeout(() => { keyBuffer = []; }, KEY_DELAY);
const next = keyBuffer.length;
if (evt.code === KONAMI_CODE[next]) {
keyBuffer.push(evt.code);
if (keyBuffer.length === KONAMI_CODE.length) {
// Change this with your desired action
alert('Konami Code successfully entered!');
}
} else {
keyBuffer = [];
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment