BlogPlaygroundOne

useHotkey API

Handle keyboard shortcuts within your application using the useHotkey composable.


Function Signature

function useHotkey(
  keys: MaybeRef<string | undefined>,
  callback: (e: KeyboardEvent) => void,
  options?: HotkeyOptions
): () => void

Parameters

Parameters
NameTypeDefaultDescription
keysMaybeRef<string | undefined>required

The key combination string or reactive reference to a key combination. Supports modifiers (ctrl, cmd, alt, shift, meta) and key sequences separated by dashes. Changes to reactive refs are automatically applied.

callback(e: KeyboardEvent) => voidrequired

Function to execute when the hotkey is triggered. Receives the KeyboardEvent as a parameter.

optionsHotkeyOptions{}

Optional configuration object to customize hotkey behavior. All options support reactive refs and will update automatically when changed.

Options

HotkeyOptions
NameTypeDefaultDescription
eventMaybeRef<'keydown' | 'keyup'>'keydown'

The keyboard event type to listen for. Can be reactive - changing this will automatically re-register the event listener.

inputsMaybeRef<boolean>false

Whether to trigger hotkeys when input elements (input, textarea, contenteditable) are focused. When false, hotkeys are disabled in input contexts. Changes to reactive refs take effect immediately.

preventDefaultMaybeRef<boolean>true

Whether to call preventDefault() on the keyboard event when the hotkey matches. Can be reactive to dynamically control event prevention.

sequenceTimeoutMaybeRef<number>1000

Timeout in milliseconds for key sequences. If the next key in a sequence isn't pressed within this time, the sequence resets. Changes to reactive refs apply to new sequences.

Return Value

Return Value
TypeDescription
() => void

Cleanup function that removes the keyboard event listener. Automatically called when the component unmounts if used within a Vue component setup context.