Files
blb-throne-of-the-bone-king/src/hooks/useEscapeKey.web.ts
T
jacob-mathison 13aa686fa8
Lint and Build Checks / Lint, Test, and Build (push) Failing after 560h41m34s
feat(gameplay): scaffold phase flow and saves
2026-06-16 10:56:06 -05:00

38 lines
854 B
TypeScript

import { useEffect } from 'react';
type KeyboardHost = typeof globalThis & {
addEventListener?: (
type: 'keydown',
listener: (event: KeyboardEvent) => void,
) => void;
removeEventListener?: (
type: 'keydown',
listener: (event: KeyboardEvent) => void,
) => void;
};
const keyboardHost = globalThis as KeyboardHost;
export function useEscapeKey(onEscape: () => void) {
useEffect(() => {
if (
keyboardHost.addEventListener == null ||
keyboardHost.removeEventListener == null
) {
return undefined;
}
function handleKeyDown(event: KeyboardEvent) {
if (event.key === 'Escape') {
onEscape();
}
}
keyboardHost.addEventListener('keydown', handleKeyDown);
return () => {
keyboardHost.removeEventListener?.('keydown', handleKeyDown);
};
}, [onEscape]);
}