<script lang="ts">
import {
createHotkey,
getFormatForDisplay,
getHotkeyManager,
getHotkeyRegistrations,
} from '@tanstack/svelte-hotkeys'
const formatForDisplay = getFormatForDisplay()
let lastCommand = $state('none')
let konamiCount = $state(0)
let helloEnabled = $state(true)
let helloCount = $state(0)
let saveCount = $state(0)
let renameCount = $state(0)
let runTestsCount = $state(0)
createHotkey(['G', 'G'], () => (lastCommand = 'Go to top'))
createHotkey(['Shift+G'], () => (lastCommand = 'Go to bottom'))
createHotkey(['D', 'D'], () => (lastCommand = 'Delete line'))
createHotkey(['Y', 'Y'], () => (lastCommand = 'Yank line'))
createHotkey(['D', 'W'], () => (lastCommand = 'Delete word'))
createHotkey(['C', 'I', 'W'], () => (lastCommand = 'Change inner word'))
createHotkey(
['ArrowUp', 'ArrowUp', 'ArrowDown', 'ArrowDown'],
() => konamiCount++,
{ timeout: 1500 },
)
createHotkey(
['H', 'E', 'L', 'L', 'O'],
() => helloCount++,
() => ({ enabled: helloEnabled }),
)
createHotkey(['Z', 'Z'], () => saveCount++, { timeout: 5000 })
createHotkey(['F2', 'R'], () => renameCount++)
createHotkey(['Shift+[KeyR]', 'Shift+[KeyT]'], () => runTestsCount++)
const registrations = getHotkeyRegistrations()
const pending = $derived(
registrations.hotkeys
.filter((reg) => reg.kind === 'sequence')
.sort((a, b) => b.matchedStepCount - a.matchedStepCount)
.at(0),
)
const pendingKeys = $derived(
pending?.matchedStepCount
? formatForDisplay(pending.hotkey.slice(0, pending.matchedStepCount))
: 'none',
)
</script>
<div class="app">
<header>
<h1>Sequences with createHotkey</h1>
<p>
Pass an array of chords to createHotkey to register a sequence, such as G
then G in Vim. Each key must follow the previous one within the timeout.
</p>
</header>
<main>
<section class="demo-section">
<h2>Vim-style commands</h2>
<table class="sequence-table">
<thead>
<tr>
<th>Keys</th>
<th>Command</th>
</tr>
</thead>
<tbody>
<tr>
<td><kbd>{formatForDisplay(['G', 'G'])}</kbd></td>
<td>Go to top</td>
</tr>
<tr>
<td><kbd>{formatForDisplay('Shift+G')}</kbd></td>
<td>Go to bottom</td>
</tr>
<tr>
<td><kbd>{formatForDisplay(['D', 'D'])}</kbd></td>
<td>Delete line</td>
</tr>
<tr>
<td><kbd>{formatForDisplay(['Y', 'Y'])}</kbd></td>
<td>Yank line</td>
</tr>
<tr>
<td><kbd>{formatForDisplay(['D', 'W'])}</kbd></td>
<td>Delete word</td>
</tr>
<tr>
<td><kbd>{formatForDisplay(['C', 'I', 'W'])}</kbd></td>
<td>Change inner word</td>
</tr>
</tbody>
</table>
<p role="status">Last command: {lastCommand}</p>
</section>
<section class="demo-section">
<h2>Set the time between steps</h2>
<p>
Press
<kbd>
{formatForDisplay(['ArrowUp', 'ArrowUp', 'ArrowDown', 'ArrowDown'])}
</kbd>
with up to 1.5 seconds between keys.
</p>
<p role="status">Konami code: {konamiCount}</p>
</section>
<section class="demo-section">
<h2>Turn a sequence on and off</h2>
<label>
<input type="checkbox" bind:checked={helloEnabled} />
Enable the hello sequence
</label>
<p>
Type <kbd>{formatForDisplay(['H', 'E', 'L', 'L', 'O'])}</kbd>.
</p>
<p role="status">Hello: {helloCount}</p>
</section>
<section class="demo-section">
<h2>Show and reset progress</h2>
<p>
Press <kbd>{formatForDisplay('Z')}</kbd> once, then reset before the
second <kbd>{formatForDisplay('Z')}</kbd>. Any other sequence in
progress shows here too.
</p>
<button
type="button"
onclick={() => getHotkeyManager().resetSequenceProgress()}
>
Reset progress
</button>
<p role="status">Pending: {pendingKeys}, Save and quit: {saveCount}</p>
</section>
<section class="demo-section">
<h2>Typing in a text field</h2>
<p>
Type <kbd>{formatForDisplay(['G', 'G'])}</kbd> in the field: it types
text and doesn't go to top. Press
<kbd>{formatForDisplay(['F2', 'R'])}</kbd> in the field to rename.
</p>
<label>
Notes <input type="text" class="demo-input" />
</label>
<p role="status">Rename: {renameCount}</p>
</section>
<section class="demo-section">
<h2>Hold Shift between steps</h2>
<p>
Press
<kbd>{formatForDisplay(['Shift+[KeyR]', 'Shift+[KeyT]'])}</kbd>. Shift
can stay down between the two keys.
</p>
<p role="status">Run tests: {runTestsCount}</p>
</section>
</main>
</div>