2023-09-22 10:01:54 +00:00
|
|
|
import { useEffect } from "react";
|
|
|
|
|
2024-05-29 12:55:03 +00:00
|
|
|
const useKeypress = (key: string, callback: (event: KeyboardEvent) => void) => {
|
2023-09-22 10:01:54 +00:00
|
|
|
useEffect(() => {
|
|
|
|
const handleKeydown = (event: KeyboardEvent) => {
|
|
|
|
if (event.key === key) {
|
2024-05-29 12:55:03 +00:00
|
|
|
callback(event);
|
2023-09-22 10:01:54 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
document.addEventListener("keydown", handleKeydown);
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
document.removeEventListener("keydown", handleKeydown);
|
|
|
|
};
|
2024-05-29 12:55:03 +00:00
|
|
|
}, [key, callback]);
|
2023-09-22 10:01:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default useKeypress;
|