Hotkeys Docs
Examples
- Kitchen Sink
- Scopes
- createHotkey
- createHotkey (sequences)
- createHotkey (platforms and alternates)
- createHotkeys
- createHotkeys (sequences)
- createHotkeyRecorder
- createHotkeyRecorder (sequences)
- createHeldKeys
- createKeyHold
- createKeyLockState
- createFormatForDisplay (SolidStart)
- createFormatForDisplay (TanStack Start)
- createFormatForDisplay (Astro)
- Keyboard Layouts
Solid Example: Keyboard Layouts
/* @refresh reload */
import { render } from '@solidjs/web'
import { TanStackDevtools } from '@tanstack/solid-devtools'
import {
HotkeysProvider,
createFormatForDisplay,
createHotkey,
} from '@tanstack/solid-hotkeys'
import { hotkeysDevtoolsPlugin } from '@tanstack/solid-hotkeys-devtools'
import { createSignal } from 'solid-js'
import type { GermanKeys, UsKeys } from '@tanstack/solid-hotkeys'
import './index.css'
// Hotkey strings accept the keys that US and German keyboards type. Without
// this, only US keys type-check, and `Mod+Ö` is an error. This changes types
// only: matching works the same with or without it.
declare module '@tanstack/solid-hotkeys' {
interface Register {
keys: UsKeys | GermanKeys
}
}
function App() {
const formatForDisplay = createFormatForDisplay()
const [lastFired, setLastFired] = createSignal('')
// These type-check because German keys are registered above.
createHotkey('Mod+Ö', () => setLastFired('Toggle comment'))
createHotkey('Mod+Shift+Ä', () => setLastFired('Add annotation'))
createHotkey('ß', () => setLastFired('Toggle sidebar'))
createHotkey('§', () => setLastFired('Show sections'))
// A to Z, 0 to 9, named keys, and physical codes type-check under every
// registration, and fire on every layout.
createHotkey('Mod+K', () => setLastFired('Open command palette'))
createHotkey('Alt+[Digit1]', () => setLastFired('Go to the first tab'))
createHotkey('Escape', () => setLastFired(''))
return (
<div class="app">
<header>
<h1>Keyboard Layouts</h1>
<p>
Registering a layout lets hotkey strings accept the keys it types,
such as Ö and ß. It changes types only: these hotkeys match the same
way without it.
</p>
</header>
<main>
<p role="status" class="note">
{lastFired() ? `Last fired: ${lastFired()}` : 'Press a hotkey below.'}
</p>
<section class="demo-section">
<h2>German keys</h2>
<p class="note">
Switch your keyboard layout to German to press these.
</p>
<div class="hotkey-list">
<div class="hotkey-row">
<kbd>{formatForDisplay('Mod+Ö')}</kbd> Toggle comment
</div>
<div class="hotkey-row">
<kbd>{formatForDisplay('Mod+Shift+Ä')}</kbd> Add annotation
</div>
<div class="hotkey-row">
<kbd>{formatForDisplay('ß')}</kbd> Toggle sidebar
</div>
<div class="hotkey-row">
<kbd>{formatForDisplay('§')}</kbd> Show sections
</div>
</div>
</section>
<section class="demo-section">
<h2>Keys on every layout</h2>
<div class="hotkey-list">
<div class="hotkey-row">
<kbd>{formatForDisplay('Mod+K')}</kbd> Open command palette
</div>
<div class="hotkey-row">
<kbd>{formatForDisplay('Alt+[Digit1]')}</kbd> Go to the first tab
</div>
<div class="hotkey-row">
<kbd>{formatForDisplay('Escape')}</kbd> Clear
</div>
</div>
</section>
<section class="demo-section">
<h2>Usage</h2>
<pre class="code-block">{`import type { GermanKeys, UsKeys } from '@tanstack/solid-hotkeys'
declare module '@tanstack/solid-hotkeys' {
interface Register {
keys: UsKeys | GermanKeys
}
}
createHotkey('Mod+Ö', toggleComment) // OK
createHotkey('Mod+[', outdent) // OK, because UsKeys is listed
createHotkey('Mod+Ñ', x) // Type error: Spanish isn't registered`}</pre>
</section>
</main>
<TanStackDevtools plugins={[hotkeysDevtoolsPlugin()]} />
</div>
)
}
const root = document.getElementById('root')!
render(
() => (
<HotkeysProvider>
<App />
</HotkeysProvider>
),
root,
)