MCPcopy Create free account
hub / github.com/TanStack/hotkeys / createHotkeyHandler

Function createHotkeyHandler

packages/hotkeys/src/match.ts:159–188  ·  view source on GitHub ↗
(
  hotkey: Hotkey | ParsedHotkey,
  callback: HotkeyCallback,
  options: CreateHotkeyHandlerOptions = {},
)

Source from the content-addressed store, hash-verified

157 * ```
158 */
159export function createHotkeyHandler(
160 hotkey: Hotkey | ParsedHotkey,
161 callback: HotkeyCallback,
162 options: CreateHotkeyHandlerOptions = {},
163): (event: KeyboardEvent) => void {
164 const { preventDefault = true, stopPropagation = true, platform } = options
165 const resolvedPlatform = platform ?? detectPlatform()
166
167 const hotkeyString: Hotkey =
168 typeof hotkey === 'string' ? hotkey : formatParsedHotkey(hotkey)
169 const parsed =
170 typeof hotkey === 'string' ? parseHotkey(hotkey, resolvedPlatform) : hotkey
171
172 const context: HotkeyCallbackContext = {
173 hotkey: hotkeyString,
174 parsedHotkey: parsed,
175 }
176
177 return (event: KeyboardEvent) => {
178 if (matchesKeyboardEvent(event, parsed, resolvedPlatform)) {
179 if (preventDefault) {
180 event.preventDefault()
181 }
182 if (stopPropagation) {
183 event.stopPropagation()
184 }
185 callback(event, context)
186 }
187 }
188}
189
190type MultiHotkeyHandler = { [K in Hotkey]?: HotkeyCallback }
191

Callers 1

match.test.tsFile · 0.90

Calls 4

detectPlatformFunction · 0.90
parseHotkeyFunction · 0.90
formatParsedHotkeyFunction · 0.85
matchesKeyboardEventFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…