Skip to content

Instantly share code, notes, and snippets.

@nihlton
Last active June 7, 2022 22:08
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nihlton/647fa58e2aaa6bd72120378e57441afd to your computer and use it in GitHub Desktop.
Save nihlton/647fa58e2aaa6bd72120378e57441afd to your computer and use it in GitHub Desktop.
useKonamiCode
const code = [
'ArrowUp', 'ArrowUp', 'ArrowDown',
'ArrowDown', 'ArrowLeft', 'ArrowRight',
'ArrowLeft', 'ArrowRight', 'KeyB', 'KeyA'
];
const useKonami = () => {
const [isActive, setActive] = useState(false);
useEffect(() => {
let cache = [];
const handler = (e) => {
cache = [...cache, e.code].slice(-code.length);
cache.join() === code.join() && setActive(true);
}
document.addEventListener('keydown', handler);
return () => document.removeEventListener('keydown', handler);
}, [])
return isActive;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment