Something went wrong. Try again.
Starship Simulator Controls thoriumsim.com
Something went wrong. Try again.
1.3 kB · 46 lines
TSX
at develop
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647import { useEffect, useRef } from "react";
export function useKonami(callback = () => {}) { const keys = useRef<string[]>([]);
const callbackRef = useRef(callback); useEffect(() => { callbackRef.current = callback; }, [callback]);
// Set up the key handler useEffect(() => { // Key handler event const downHandler = (e: KeyboardEvent) => { let key: string | null = null; if (e.code === "ArrowUp") key = "u"; if (e.code === "ArrowDown") key = "d"; if (e.code === "ArrowLeft") key = "l"; if (e.code === "ArrowRight") key = "r"; if (e.code === "KeyA") key = "a"; if (e.code === "KeyB") key = "b";
if (key) { // Slice it down to the correct size if (keys.current.length >= 10) { keys.current = keys.current.concat(key).slice(1, 11); } else { keys.current = keys.current.concat(key); } if (keys.current.join("") === "uuddlrlrba") { callbackRef.current?.(); keys.current = []; } } else { // If there is a stray key, cancel the whole konami code keys.current = []; } };
window.addEventListener("keydown", downHandler, { passive: true }); // Remove event listeners on cleanup return () => { window.removeEventListener("keydown", downHandler); }; }, []);}