Shortcut engine

Register a keyboard shortcut for as long as a component exists, and show it with the keys that fit the platform. Shortcuts show up in the shortcut overview of AppShell.

What it does
Registers keyboard shortcuts for as long as a component exists, shows them with the keys of the platform, and keeps the ones behind an overlay quiet.
Exported from the package?
Yes. Import useShortcut, shortcutTrap, getShortcuts, assembleShortcut and getEventId from @davidnet-net/svelte-ui.

Which one do I use?

What I needUse
Add a shortcut to a componentuseShortcut
Silence the shortcuts behind a modalshortcutTrap
List the shortcuts, or get the keys of a combogetShortcuts, assembleShortcut
Keep Tab inside the modalFocus engine

useShortcut

typescript
typescript
function useShortcut(
	combo: string | (() => string),
	callback: (e: KeyboardEvent) => void,
	options?: ShortcutOptions
): { readonly keys: string[]; readonly original: string }

useShortcut(combo, callback, options) registers a shortcut like "ctrl+k" or "e". Call it while a component is created (it uses an effect). It returns keys, the keys as they should be shown (⌘, Ctrl), and original. combo may be a function, so the shortcut follows state. When two components register the same keys, the one registered last wins.

When to use

Use it for an action people repeat often, and show keys next to the control so people learn it.

When not to use, and what to use instead

A shortcut is never the only way: every action needs a button or a menu item too. By default a plain character (like e) does nothing while someone types in a field, and Escape, Enter, arrows and combinations with Ctrl, Alt or Meta always work. Set allowInInput only if you are sure. For an overlay that must keep the keys behind it quiet, use shortcutTrap.

Options

NameTypeDefaultDescription
preventDefault booleantrueCalls preventDefault on the event before your callback.
name string—A name for the shortcut overview.
description string—A description for the shortcut overview.
active boolean | (() => boolean)trueWhether the shortcut works now. Pass a function to make it follow state.
allowInInput booleanfalseWhether a plain character also works while someone types in a text field.

Live example

Press these keys anywhere on this page, or use the button. The counter goes up each time.

Alt +Shift +J
Result Times the shortcut ran: 0

Type the letter e here: nothing happens, because you are typing. Click outside the field and press e.

Result Times e ran: 0
svelte
svelte
<script lang="ts">
	import { useShortcut } from "@davidnet-net/svelte-ui";

	let count = $state(0);

	// Call it while the component is created. It stops when the component goes away.
	const save = useShortcut("ctrl+s", () => count++, {
		name: "Save",
		description: "Save the changes"
	});
</script>

<button onclick={() => count++}>Save <kbd>{save.keys.join("+")}</kbd></button>

shortcutTrap

typescript
typescript
function shortcutTrap(node: HTMLElement, active?: boolean): ActionReturn

shortcutTrap is an action: use:shortcutTrap on an element blocks every shortcut that was registered before the trap became active. Shortcuts registered inside it keep working. Removing the element, or setting active to false, lets the others work again.

When to use

Use it on a modal, a drawer or a menu, so a shortcut of the page behind it does not run. Combine it with focusTrap, which keeps the focus inside.

When not to use, and what to use instead

It does not move or hold the focus: that is focusTrap. It does not remove shortcuts, it only holds back the older ones while it is active.

Live example

While the trap is on, e does nothing.
Result The trap is off: e works. Times e ran: 0
svelte
svelte
<div use:shortcutTrap={open}>
	<!-- shortcuts of the page behind this element are quiet while open is true -->
</div>

getShortcuts

typescript
typescript
function getShortcuts(): { id: string; display: string[]; original: string; name?: string; description?: string }[]
function assembleShortcut(input: string): { id: string; display: string[]; original: string }
function getEventId(e: KeyboardEvent): string

getShortcuts() lists the active shortcuts with their id, display, original, name and description. assembleShortcut(combo) turns a text like "ctrl+k" into its id and the keys to show, and getEventId(event) gives the same id for a keyboard event.

When to use

Use getShortcuts for your own shortcut overview, and assembleShortcut to show keys for something you do not register with useShortcut.

When not to use, and what to use instead

Do not use getShortcuts to run a shortcut: it only lists them. Do not parse a combo yourself: assembleShortcut knows the aliases (cmd, esc) and the keys of the platform.

Live example

CallResult
assembleShortcut("ctrl+k").displayCtrl + K
getShortcuts().map((s) => s.original)""