diff --git a/apps/frontend/src/hooks/useHotkey.ts b/apps/frontend/src/hooks/useHotkey.ts new file mode 100644 index 00000000..14d9269d --- /dev/null +++ b/apps/frontend/src/hooks/useHotkey.ts @@ -0,0 +1,18 @@ +import { useEffect } from 'react'; + +export const useHotkey = (keyCode: string, callback: () => void) => { + useEffect(() => { + const handleKeyDown = (event: KeyboardEvent) => { + if (event.code === keyCode && document.activeElement === document.body) { + event.preventDefault(); + callback(); + } + }; + + window.addEventListener('keydown', handleKeyDown); + + return () => { + window.removeEventListener('keydown', handleKeyDown); + }; + }, [callback, keyCode]); +};