<script lang="ts">
import {
createHotkeyRecorder,
createHotkeys,
getFormatForDisplay,
} from '@tanstack/svelte-hotkeys'
import type { Hotkey } from '@tanstack/svelte-hotkeys'
const formatForDisplay = getFormatForDisplay()
function hotkeyText(hotkey: Hotkey) {
return Array.isArray(hotkey) ? hotkey.join(' ') : hotkey
}
let listStatus = $state('Not recording')
let editing = $state<string | null>(null)
let shortcuts = $state<Array<{ name: string; hotkey: Hotkey | null }>>([
{ name: 'Save', hotkey: 'Mod+S' },
{ name: 'Go to inbox', hotkey: ['G', 'I'] },
{ name: 'Go to settings', hotkey: ['G', 'S'] },
])
const recorder = createHotkeyRecorder({
maxSteps: Infinity,
onRecord: (hotkey) => {
const row = shortcuts.find((s) => s.name === editing)
if (row) row.hotkey = hotkey
listStatus = `${editing} is now ${hotkeyText(hotkey)}`
editing = null
},
onCancel: () => {
listStatus = `${editing} unchanged`
editing = null
},
onClear: () => {
const row = shortcuts.find((s) => s.name === editing)
if (row) row.hotkey = null
listStatus = `${editing} cleared`
editing = null
},
})
createHotkeys(() =>
shortcuts.map((s) => ({
hotkey: s.hotkey,
callback: () => (listStatus = `Ran ${s.name}`),
options: { meta: { name: s.name } },
})),
)
let maxStepsStatus = $state('Not recording')
const twoSteps = createHotkeyRecorder({
maxSteps: 2,
onRecord: (hotkey) => (maxStepsStatus = `Recorded ${hotkeyText(hotkey)}`),
onCancel: () => (maxStepsStatus = 'Cancelled'),
})
let commitStatus = $state('Not recording')
const enterSaves = createHotkeyRecorder({
maxSteps: Infinity,
commitKeys: 'enter',
onRecord: (hotkey) => (commitStatus = `Recorded ${hotkeyText(hotkey)}`),
onCancel: () => (commitStatus = 'Cancelled'),
})
const enterRecords = createHotkeyRecorder({
maxSteps: Infinity,
commitKeys: 'none',
onRecord: (hotkey) => (commitStatus = `Recorded ${hotkeyText(hotkey)}`),
onCancel: () => (commitStatus = 'Cancelled'),
})
let idleStatus = $state('Not recording')
const idleSaves = createHotkeyRecorder({
maxSteps: Infinity,
idleTimeoutMs: 1000,
onRecord: (hotkey) => (idleStatus = `Recorded ${hotkeyText(hotkey)}`),
onCancel: () => (idleStatus = 'Cancelled'),
})
let validationStatus = $state('Not recording')
const validated = createHotkeyRecorder({
maxSteps: Infinity,
validate: (_hotkey, context) =>
context.kind === 'sequence' ||
context.parsedChord.modifiers.length > 0 ||
'A single chord needs a modifier.',
onReject: ({ reason, message }) =>
(validationStatus = message
? `Rejected: ${message}`
: `Rejected (${reason})`),
onRecord: (hotkey) => (validationStatus = `Recorded ${hotkeyText(hotkey)}`),
onCancel: () => (validationStatus = 'Cancelled'),
})
let conflictsStatus = $state('Not recording')
const conflictChecked = createHotkeyRecorder({
maxSteps: Infinity,
detectConflicts: true,
onReject: ({ reason, hotkey, conflicts }) =>
(conflictsStatus =
reason === 'conflict' && hotkey
? `Rejected: ${hotkeyText(hotkey)} is used by ${conflicts
?.map((c) => c.registration.options.meta?.name)
.join(', ')}`
: `Rejected (${reason})`),
onRecord: (hotkey) => (conflictsStatus = `Recorded ${hotkeyText(hotkey)}`),
onCancel: () => (conflictsStatus = 'Cancelled'),
})
</script>
<div class="app">
<header>
<h1>Recording Sequences</h1>
<p>
With <code>maxSteps</code> above 1, <code>createHotkeyRecorder</code>
records several chords in a row, such as G then I. Each section shows one recorder
option.
</p>
</header>
<main>
<section class="demo-section">
<h2>Shortcut list</h2>
<p>
Click Edit, press each chord in order, then press Enter. Backspace
removes the last chord, or clears the shortcut when none are left.
Escape cancels.
</p>
<ul class="shortcut-list">
{#each shortcuts as s (s.name)}
<li>
<span>{s.name}</span
>{#if editing === s.name && recorder.isRecording}
{#if recorder.steps.length > 0}
<kbd>{formatForDisplay(recorder.steps)}</kbd>
{:else}
<span>Press keys</span>
{/if}
{:else if s.hotkey}
<kbd>{formatForDisplay(s.hotkey)}</kbd>
{:else}
<span>No shortcut</span>
{/if}{#if editing === s.name && recorder.isRecording}
<button type="button" onclick={recorder.cancelRecording}>
Cancel
</button>
{:else}
<button
type="button"
onclick={() => {
recorder.startRecording()
editing = s.name
listStatus = `Recording ${s.name}`
}}
>
Edit
</button>
{/if}
</li>
{/each}
</ul>
<p role="status" class="status">{listStatus}</p>
</section>
<section class="demo-section">
<h2>Max steps</h2>
<p>
Press <kbd>{formatForDisplay('G')}</kbd>, then
<kbd>{formatForDisplay('H')}</kbd>. Recording ends at the second chord
without Enter.
</p>
<div class="buttons">
<button
type="button"
onclick={() => {
twoSteps.startRecording()
maxStepsStatus = 'Recording'
}}
>
Record
</button>
</div>
<p role="status" class="status">{maxStepsStatus}</p>
</section>
<section class="demo-section">
<h2>Commit keys</h2>
<p>
Press <kbd>{formatForDisplay('G')}</kbd>, then
<kbd>{formatForDisplay('Enter')}</kbd> with each button. With commitKeys:
'none', click Done to save.
</p>
<div class="buttons">
<button
type="button"
onclick={() => {
enterSaves.startRecording()
commitStatus = 'Recording'
}}
>
Record (commitKeys: 'enter')
</button>
<button
type="button"
onclick={() => {
enterRecords.startRecording()
commitStatus = 'Recording'
}}
>
Record (commitKeys: 'none')
</button>
<button
type="button"
disabled={enterRecords.steps.length === 0}
onclick={enterRecords.commitRecording}
>
Done
</button>
</div>
{#if enterRecords.steps.length > 0}
<p>Steps: <kbd>{formatForDisplay(enterRecords.steps)}</kbd></p>
{/if}
<p role="status" class="status">{commitStatus}</p>
</section>
<section class="demo-section">
<h2>Idle timeout</h2>
<p>
Press <kbd>{formatForDisplay('G')}</kbd>, then
<kbd>{formatForDisplay('H')}</kbd>, and wait one second.
</p>
<div class="buttons">
<button
type="button"
onclick={() => {
idleSaves.startRecording()
idleStatus = 'Recording'
}}
>
Record
</button>
</div>
{#if idleSaves.steps.length > 0}
<p>Steps: <kbd>{formatForDisplay(idleSaves.steps)}</kbd></p>
{/if}
<p role="status" class="status">{idleStatus}</p>
</section>
<section class="demo-section">
<h2>Validation</h2>
<p>
Press <kbd>{formatForDisplay('G')}</kbd>, then
<kbd>{formatForDisplay('Enter')}</kbd> to see the rejection. Then press
<kbd>{formatForDisplay('H')}</kbd> and
<kbd>{formatForDisplay('Enter')}</kbd>.
</p>
<div class="buttons">
<button
type="button"
onclick={() => {
validated.startRecording()
validationStatus = 'Recording'
}}
>
Record
</button>
</div>
{#if validated.steps.length > 0}
<p>Steps: <kbd>{formatForDisplay(validated.steps)}</kbd></p>
{/if}
<p role="status" class="status">{validationStatus}</p>
</section>
<section class="demo-section">
<h2>Conflicts</h2>
<p>
Record <kbd>{formatForDisplay('G')}</kbd> alone, or
<kbd>{formatForDisplay(['G', 'I'])}</kbd>, to conflict with the list
above. Then record <kbd>{formatForDisplay(['G', 'H'])}</kbd>.
</p>
<div class="buttons">
<button
type="button"
onclick={() => {
conflictChecked.startRecording()
conflictsStatus = 'Recording'
}}
>
Record
</button>
</div>
{#if conflictChecked.steps.length > 0}
<p>Steps: <kbd>{formatForDisplay(conflictChecked.steps)}</kbd></p>
{/if}
<p role="status" class="status">{conflictsStatus}</p>
</section>
</main>
</div>