MCPcopy
hub / github.com/codeaashu/claude-code / useKeybinding

Function useKeybinding

src/keybindings/useKeybinding.ts:33–97  ·  view source on GitHub ↗
(
  action: string,
  handler: () => void | false | Promise<void>,
  options: Options = {},
)

Source from the content-addressed store, hash-verified

31 * ```
32 */
33export function useKeybinding(
34 action: string,
35 handler: () => void | false | Promise<void>,
36 options: Options = {},
37): void {
38 const { context = 'Global', isActive = true } = options
39 const keybindingContext = useOptionalKeybindingContext()
40
41 // Register handler with the context for ChordInterceptor to invoke
42 useEffect(() => {
43 if (!keybindingContext || !isActive) return
44 return keybindingContext.registerHandler({ action, context, handler })
45 }, [action, context, handler, keybindingContext, isActive])
46
47 const handleInput = useCallback(
48 (input: string, key: Key, event: InputEvent) => {
49 // If no keybinding context available, skip resolution
50 if (!keybindingContext) return
51
52 // Build context list: registered active contexts + this context + Global
53 // More specific contexts (registered ones) take precedence over Global
54 const contextsToCheck: KeybindingContextName[] = [
55 ...keybindingContext.activeContexts,
56 context,
57 'Global',
58 ]
59 // Deduplicate while preserving order (first occurrence wins for priority)
60 const uniqueContexts = [...new Set(contextsToCheck)]
61
62 const result = keybindingContext.resolve(input, key, uniqueContexts)
63
64 switch (result.type) {
65 case 'match':
66 // Chord completed (if any) - clear pending state
67 keybindingContext.setPendingChord(null)
68 if (result.action === action) {
69 if (handler() !== false) {
70 event.stopImmediatePropagation()
71 }
72 }
73 break
74 case 'chord_started':
75 // User started a chord sequence - update pending state
76 keybindingContext.setPendingChord(result.pending)
77 event.stopImmediatePropagation()
78 break
79 case 'chord_cancelled':
80 // Chord was cancelled (escape or invalid key)
81 keybindingContext.setPendingChord(null)
82 break
83 case 'unbound':
84 // Explicitly unbound - clear any pending chord
85 keybindingContext.setPendingChord(null)
86 event.stopImmediatePropagation()
87 break
88 case 'none':
89 // No match - let other handlers try
90 break

Callers 15

BackgroundHintFunction · 0.85
ResumeTaskFunction · 0.85
SessionBackgroundHintFunction · 0.85
ConsoleOAuthFlowFunction · 0.85
SingleEnvironmentContentFunction · 0.85
SessionPreviewFunction · 0.85
TeleportResumeWrapperFunction · 0.85
MessageSelectorFunction · 0.85
ThinkingToggleFunction · 0.85
LogSelectorFunction · 0.85
LanguagePickerFunction · 0.85
ThemePickerFunction · 0.85

Calls 6

useInputFunction · 0.85
registerHandlerMethod · 0.80
handlerFunction · 0.50
resolveMethod · 0.45

Tested by

no test coverage detected