Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
25 lines (21 sloc) 518 Bytes
import { useEffect } from 'react';
interface IKeyMap {
[key: string]: Function;
}
export default function useKeyboardShortcut(keyMap: IKeyMap) {
useEffect(() => {
function handleKeyPress(e: KeyboardEvent) {
if (!e.ctrlKey) {
return;
}
const callback = keyMap[e.key];
if (callback) {
callback();
}
}
document.addEventListener('keypress', handleKeyPress);
return () => {
document.removeEventListener('keypress', handleKeyPress);
};
});
}
You can’t perform that action at this time.