MCPcopy Create free account
hub / github.com/msgbyte/tailchat / useGlobalKeyDown

Function useGlobalKeyDown

client/web/src/hooks/useGlobalKeyDown.ts:8–25  ·  view source on GitHub ↗
(
  fn: (e: KeyboardEvent) => void,
  options?: AddEventListenerOptions
)

Source from the content-addressed store, hash-verified

6 * 仅接受最初的函数
7 */
8export function useGlobalKeyDown(
9 fn: (e: KeyboardEvent) => void,
10 options?: AddEventListenerOptions
11) {
12 const fnRef = useUpdateRef(fn);
13
14 useLayoutEffect(() => {
15 const handleKeyDown = (e: KeyboardEvent) => {
16 typeof fnRef.current === 'function' && fnRef.current(e);
17 };
18
19 window.addEventListener('keydown', handleKeyDown, options);
20
21 return () => {
22 window.removeEventListener('keydown', handleKeyDown, options);
23 };
24 }, []);
25}

Callers 4

usePasteHandlerFunction · 0.90
index.tsxFile · 0.90
useShortcutsFunction · 0.90

Calls 1

useUpdateRefFunction · 0.85

Tested by

no test coverage detected