agb/website/app/src/useOnKeyUp.hook.ts
2024-04-04 00:54:23 +01:00

18 lines
418 B
TypeScript

import { useEffect } from "react";
export const useOnKeyUp = (targetKey: string, callback: () => void) => {
useEffect(() => {
const downHandler = (evnt: KeyboardEvent) => {
if (evnt.key === targetKey) {
callback();
}
};
window.addEventListener("keyup", downHandler);
return () => {
window.removeEventListener("keyup", downHandler);
};
}, [callback, targetKey]);
};