TanStack

createHotkeyHandler

ts
function createHotkeyHandler(
   hotkey, 
   callback, 
   options?): (event) => void;

Defined in: match.ts:87

Creates a keyboard event handler that calls the callback when the hotkey matches.

Parameters

hotkey

| Hotkey | ParsedHotkey

The hotkey string or ParsedHotkey to match

callback

HotkeyCallback

The function to call when the hotkey matches

options?

CreateHotkeyHandlerOptions = {}

Options for matching and handling

Returns

A function that can be used as an event handler

(event) => void

Example

ts
const handler = createHotkeyHandler('Mod+S', (event, { hotkey, parsedHotkey }) => {
  console.log(`${hotkey} was pressed`)
  handleSave()
})

document.addEventListener('keydown', handler)