|
|
|
@ -5,7 +5,10 @@ import { useUpdateRef } from 'tailchat-shared';
|
|
|
|
|
* keydown hooks
|
|
|
|
|
* 仅接受最初的函数
|
|
|
|
|
*/
|
|
|
|
|
export function useGlobalKeyDown(fn: (e: KeyboardEvent) => void) {
|
|
|
|
|
export function useGlobalKeyDown(
|
|
|
|
|
fn: (e: KeyboardEvent) => void,
|
|
|
|
|
options?: AddEventListenerOptions
|
|
|
|
|
) {
|
|
|
|
|
const fnRef = useUpdateRef(fn);
|
|
|
|
|
|
|
|
|
|
useLayoutEffect(() => {
|
|
|
|
@ -13,10 +16,10 @@ export function useGlobalKeyDown(fn: (e: KeyboardEvent) => void) {
|
|
|
|
|
typeof fnRef.current === 'function' && fnRef.current(e);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
window.addEventListener('keydown', handleKeyDown);
|
|
|
|
|
window.addEventListener('keydown', handleKeyDown, options);
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
window.removeEventListener('keydown', handleKeyDown);
|
|
|
|
|
window.removeEventListener('keydown', handleKeyDown, options);
|
|
|
|
|
};
|
|
|
|
|
}, []);
|
|
|
|
|
}
|
|
|
|
|