shellphone.app/app/features/keypad/hooks/use-key-press.ts
2022-05-14 12:22:06 +02:00

18 lines
395 B
TypeScript

import { useCallback, useEffect } from "react";
export default function useKeyPress(onKeyPress: (key: string) => void) {
const onKeyDown = useCallback(
({ key }: KeyboardEvent) => {
onKeyPress(key);
},
[onKeyPress],
);
useEffect(() => {
window.addEventListener("keydown", onKeyDown);
return () => {
window.removeEventListener("keydown", onKeyDown);
};
}, [onKeyDown]);
}