Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
29 lines (27 sloc) 811 Bytes
import { useEffect } from "react";
// Hook implementation inspired by https://medium.com/@nicolaslopezj/reusing-logic-with-react-hooks-8e691f7352fa
export function useKeyboardEvent(
keys: string[],
callback: (key: string) => void
) {
useEffect(() => {
const handler = function(event: any) {
const key = event.key;
if (key) {
if (keys.includes(key)) {
// Fire the callback with the given key
callback(key);
}
// Special case for Del key (map to Backspace)
const deleteKey = "DEL";
if (key === "Backspace" && keys.includes(deleteKey)) {
callback(deleteKey);
}
}
};
window.addEventListener("keydown", handler);
return () => {
window.removeEventListener("keydown", handler);
};
});
}
You can’t perform that action at this time.