Skip to content

createHotkeyAttachment

ts
function createHotkeyAttachment<T>(
   hotkey, 
   callback, 
   options?
): Attachment<HTMLElement>;

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

Create an attachment for an element-scoped hotkey: a chord, a sequence, or an app hotkey that listens on the attached element. undefined, null, or '' registers nothing, for an optional shortcut.

Type Parameters

T

T extends AdapterHotkeyInput

Parameters

hotkey

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

callback

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

options?

IfTypeParameter<T, MaybeGetter<CreateHotkeyOptions>, MaybeGetter<[T] extends [AppHotkey<string, AppHotkeyBinding>] ? AdapterAppHotkeyOptions<Extract<T, AppHotkey<string, AppHotkeyBinding>>, CreateHotkeyTarget> : AdapterOptionsFor<T, CreateHotkeyTarget>>>

Returns

Attachment<HTMLElement>

Example

svelte
<script lang="ts">
  const saveKey = createHotkeyAttachment('Mod+S', () => save())
</script>

<div tabindex="0" {@attach saveKey}>Editor content</div>