mirror of
https://github.com/whscullin/apple2js.git
synced 2024-01-12 14:14:38 +00:00
1e79d9d59d
* Enabled prettier * Update lint, fix issues * Restore some array formatting
29 lines
761 B
TypeScript
29 lines
761 B
TypeScript
import { useEffect } from 'preact/hooks';
|
|
|
|
/**
|
|
* Hook to allow registering hotkey that will automatically
|
|
* be cleaned up when they leave scope
|
|
*
|
|
* @param key KeyboardEvent key value to match
|
|
* @param callback Invoked when key is pressed
|
|
*/
|
|
export const useHotKey = (
|
|
key: string,
|
|
callback: (ev: KeyboardEvent) => void
|
|
) => {
|
|
useEffect(() => {
|
|
const onKeyDown = (ev: KeyboardEvent) => {
|
|
if (ev.key === key) {
|
|
ev.preventDefault();
|
|
ev.stopPropagation();
|
|
callback(ev);
|
|
}
|
|
};
|
|
document.addEventListener('keydown', onKeyDown);
|
|
|
|
return () => {
|
|
document.removeEventListener('keydown', onKeyDown);
|
|
};
|
|
}, [key, callback]);
|
|
};
|