Skip to content

Instantly share code, notes, and snippets.

@liamnewmarch
Last active February 14, 2020 09:45
Show Gist options
  • Save liamnewmarch/e038e6469588326b802d to your computer and use it in GitHub Desktop.
Save liamnewmarch/e038e6469588326b802d to your computer and use it in GitHub Desktop.
Listens for the Konami code, runs a callback when detected.
/**
* Konami
*
* Listens for the [Konami code][1], runs a callback when detected.
*
* [1]: https://en.wikipedia.org/wiki/Konami_Code
*
* Example usage:
*
* import { konami } from './konami.js/';
*
* konami(() => {
* console.log('Hello, Konami!');
* });
*
*/
export function konami(fn, {
pattern = [38, 38, 40, 40, 37, 39, 37, 39, 66, 65],
} = {}) {
const keys = [];
window.addEventListener('keydown', ({ keyCode }) => {
keys.push(keyCode);
if (keys.length > pattern.length) keys.splice(0, 1);
if (keys.join(',') === pattern.join(',')) fn();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment