MCPcopy Create free account
hub / github.com/AnukarOP/claude-code-leaked / useKeybinding

Function useKeybinding

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

Source from the content-addressed store, hash-verified

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

useInputFunction · 0.85
resolveMethod · 0.80

Tested by

no test coverage detected