Docs
Cloudflare
Railway
CodeRabbit
SerpAPI
Clerk
WorkOS
OpenRouter
Netlify
AG Grid
Electric
Prisma
Unkey
Sentry
Cloudflare
Railway
CodeRabbit
SerpAPI
Clerk
WorkOS
OpenRouter
Netlify
AG Grid
Electric
Prisma
Unkey
Sentry
API Reference
Hotkeys API Reference
Hotkey Sequence API Reference
Key hold & held keys API Reference
Hotkey Recorder API Reference
Hotkey Sequence Recorder API Reference
Normalization & format API Reference
Hotkeys API Reference

createHotkeyAttachment

Function: createHotkeyAttachment()

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

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

Create an attachment for element-scoped hotkeys.

Parameters

hotkey

MaybeGetter<RegisterableHotkey>

callback

HotkeyCallback

options

MaybeGetter<CreateHotkeyOptions> = {}

Returns

Attachment<HTMLElement>

Example

svelte
<script lang="ts">
  import { createHotkeyAttachment } from '@tanstack/svelte-hotkeys'

  let count = $state(0)
  const saveHotkey = createHotkeyAttachment('Mod+S', () => {
    count++
  })

</script>

<div tabindex="0" {@attach saveHotkey}>
  Count: {count}
</div>
<script lang="ts">
  import { createHotkeyAttachment } from '@tanstack/svelte-hotkeys'

  let count = $state(0)
  const saveHotkey = createHotkeyAttachment('Mod+S', () => {
    count++
  })

</script>

<div tabindex="0" {@attach saveHotkey}>
  Count: {count}
</div>