lots of UI changes, and keyboard command support
This commit is contained in:
20
src/views/hooks/useKeyPress.ts
Normal file
20
src/views/hooks/useKeyPress.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { useEffect } from 'react';
|
||||
|
||||
/**
|
||||
* Hook that calls a callback when a key is pressed
|
||||
* @param key the key to listen for
|
||||
* @param callback the callback to call when the key is pressed
|
||||
*/
|
||||
export function useKeyPress(key: string, callback: (...args: any[]) => void): void {
|
||||
useEffect(() => {
|
||||
const handleKeyDown = (event: KeyboardEvent) => {
|
||||
if (event.key === key) {
|
||||
callback();
|
||||
}
|
||||
};
|
||||
document.addEventListener('keydown', handleKeyDown);
|
||||
return () => {
|
||||
document.removeEventListener('keydown', handleKeyDown);
|
||||
};
|
||||
}, [key, callback]);
|
||||
}
|
||||
Reference in New Issue
Block a user