Skip to content

useHotkey

ts
function useHotkey<T>(
   hotkey, 
   callback, 
   options?
): void;

Defined in: useHotkey.ts:93

Vue composable for registering a hotkey: a chord such as 'Mod+S', a sequence such as ['G', 'G'], or an app hotkey from defineAppHotkeys. The hotkey and options may be refs or getters.

Chords accept requireReset; sequences accept timeout. A one-step sequence registers as its chord. An app hotkey uses its effective hotkey from HotkeysProvider (including the user's custom hotkey) and its declared meta and options, and registers nothing while unbound. undefined, null, or '' registers nothing, for an optional shortcut.

Type Parameters

T

T extends AdapterHotkeyInput

Parameters

hotkey

IfTypeParameter<T, MaybeRefOrGetter<T | null | undefined>, KnownHotkey<T, MaybeRefOrGetter<T | null | undefined>>>

callback

HotkeyCallback<IfTypeParameter<T, HotkeyCallbackContext, AdapterCallbackContextFor<T>>>

options?

IfTypeParameter<T, MaybeRefOrGetter<Omit<AdapterHotkeyOptions<UseHotkeyTarget>, "enabled"> & object>, MaybeRefOrGetter<[T] extends [AppHotkey<string, AppHotkeyBinding>] ? WithReactiveEnabled<AdapterAppHotkeyOptions<Extract<T, AppHotkey<string, AppHotkeyBinding>>, UseHotkeyTarget>> : WithReactiveEnabled<AdapterOptionsFor<T, UseHotkeyTarget>>>>

Returns

void

Example

ts
useHotkey('Mod+S', () => save())
useHotkey(['G', 'G'], () => scrollToTop(), { timeout: 500 })
useHotkey('Escape', () => close(), { enabled: isOpen })