Tiny, lightweight, cross-browser compatible. Only 461 bytes
minified!
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 = []; | |
} | |
}); |