svelte-attach

Keyboard

API docs
import { hotkey, pressed, formatHint } from "@joknoll/svelte-attach/key";

Bind a shortcut

Shortcuts work without focus. By default, they're ignored in text fields.

<button {@attach hotkey("f")}>❤️ Like</button>

Hold to repeat

Hold an arrow key or H, J, K, or L.

Row 3, column 3
<button
  {@attach hotkey(["arrowup", "k"], {
    ignoreRepeat: false,
    pressedClass: "is-pressed",
  })}
  onclick={() => move(0, -1)}
>↑</button>

Shortcut hints

"shift"
Shift
"mod+enter"
Ctrl + Enter
"alt+arrowup"
Alt + Up
["j", "arrowdown"]
J / Down
<kbd {@attach pressed("mod+enter")}>{formatHint("mod+enter")}</kbd>

Disable a shortcut

Press T to toggle the shortcut. Next stays clickable.

Step 1
<button {@attach enabled && hotkey("n")}>Next</button>