TanStack
Hotkeys API Reference

createHotkey

Call Signature

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

Defined in: packages/svelte-hotkeys/src/createHotkey.svelte.ts:63

Svelte function for registering a hotkey on the document: a chord such as 'Mod+S', a sequence such as ['G', 'G'], or an app hotkey from defineAppHotkeys. Pass getters for reactive values. Use createHotkeyAttachment for element-scoped hotkeys.

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.

Type Parameters

T

T extends AppHotkey<string, HotkeyChord | HotkeySequence | null>

Parameters

hotkey

MaybeGetter<T>

callback

HotkeyCallback

options?

MaybeGetter<CreateAppHotkeyOptions<T>>

Returns

void

Example

svelte
<script lang="ts">
  createHotkey('Mod+S', () => save())
  createHotkey(['G', 'G'], () => scrollToTop(), { timeout: 500 })
  createHotkey('Escape', () => close(), () => ({ enabled: isOpen }))
</script>

Call Signature

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

Defined in: packages/svelte-hotkeys/src/createHotkey.svelte.ts:68

Svelte function for registering a hotkey on the document: a chord such as 'Mod+S', a sequence such as ['G', 'G'], or an app hotkey from defineAppHotkeys. Pass getters for reactive values. Use createHotkeyAttachment for element-scoped hotkeys.

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.

Type Parameters

T

T extends RegisterableHotkey

Parameters

hotkey

MaybeGetter<T>

callback

HotkeyCallback<HotkeyCallbackContextFor<T>>

options?

MaybeGetter<AdapterOptionsFor<T, CreateHotkeyTarget>>

Returns

void

Example

svelte
<script lang="ts">
  createHotkey('Mod+S', () => save())
  createHotkey(['G', 'G'], () => scrollToTop(), { timeout: 500 })
  createHotkey('Escape', () => close(), () => ({ enabled: isOpen }))
</script>