<script lang="ts">
import {
createHotkey,
getFormatForDisplay,
getHeldKeyCodesMap,
getHeldKeys,
getHotkeyHint,
} from '@tanstack/svelte-hotkeys'
const formatForDisplay = getFormatForDisplay()
const heldKeys = getHeldKeys()
const heldCodes = getHeldKeyCodesMap()
let history = $state<Array<string>>([])
let lastAction = $state('none')
let boldEnabled = $state(true)
let mostHeld: Array<string> = []
$effect(() => {
const keys = heldKeys.keys
if (keys.length > mostHeld.length) mostHeld = keys
if (keys.length > 0 || mostHeld.length === 0) return
const combination = mostHeld.join(' + ')
mostHeld = []
history = [...history.slice(-9), combination]
})
createHotkey('Mod+S', () => (lastAction = 'Save'))
createHotkey(
'Mod+B',
() => (lastAction = 'Bold'),
() => ({ enabled: boldEnabled }),
)
createHotkey('Mod+Shift+E', () => (lastAction = 'Export'))
createHotkey('Mod+Shift+A', () => (lastAction = 'Archive'))
const saveHint = getHotkeyHint('Mod+S')
const boldHint = getHotkeyHint('Mod+B')
const exportHint = getHotkeyHint('Mod+Shift+E')
const archiveHint = getHotkeyHint('Mod+Shift+A', { exact: true })
const shownHints = $derived(
[
saveHint.visible && 'Save',
boldHint.visible && 'Bold',
exportHint.visible && 'Export',
archiveHint.visible && 'Archive',
].filter(Boolean),
)
</script>
<div class="app">
<header>
<h1>getHeldKeys</h1>
<p>
Track which keys the user is holding, and show a hotkey's label only while
its modifiers are held.
</p>
</header>
<main>
<section class="demo-section">
<h2>Held keys</h2>
<p>
Hold <kbd>Shift</kbd> + <kbd>Control</kbd> + <kbd>A</kbd>, or press
several letters at once, then release them one by one.
</p>
<div class="key-display" aria-hidden="true">
{#each heldKeys.keys as key (key)}
<kbd class="large">{formatForDisplay({ key })}</kbd>
{/each}
</div>
<p role="status">
{heldKeys.keys.length > 0
? `Held: ${heldKeys.keys.join(' + ')}`
: 'No keys held'}
</p>
</section>
<section class="demo-section">
<h2>Held key codes</h2>
<p>
The physical key behind each held key. Left and right <kbd>Shift</kbd>
both hold <code>Shift</code>, with different codes.
</p>
<p role="status">
{heldKeys.keys.length > 0
? heldKeys.keys
.map((key) => `${key}: ${heldCodes.codes[key]}`)
.join(', ')
: 'No keys held'}
</p>
</section>
<section class="demo-section">
<h2>Hotkey hints</h2>
<p>
Hold <kbd>{formatForDisplay('Mod')}</kbd>, <kbd>Shift</kbd>, or both to
see which shortcuts they lead to.
</p>
<div class="toolbar">
<button onclick={() => (lastAction = 'Save')}>
Save {#if saveHint.visible}<kbd>{formatForDisplay('Mod+S')}</kbd>{/if}
</button>
<button onclick={() => (lastAction = 'Bold')}>
Bold {#if boldHint.visible}<kbd>{formatForDisplay('Mod+B')}</kbd>{/if}
</button>
<button onclick={() => (lastAction = 'Export')}>
Export {#if exportHint.visible}<kbd
>{formatForDisplay('Mod+Shift+E')}</kbd
>{/if}
</button>
<button onclick={() => (lastAction = 'Archive')}>
Archive {#if archiveHint.visible}<kbd
>{formatForDisplay('Mod+Shift+A')}</kbd
>{/if}
</button>
</div>
<label>
<input type="checkbox" bind:checked={boldEnabled} />
Enable Bold
</label>
<p>Last action: {lastAction}</p>
<p role="status">
{shownHints.length > 0
? `Hints shown: ${shownHints.join(', ')}`
: 'Hints shown: none'}
</p>
</section>
<section class="demo-section">
<h2>Recent combinations</h2>
<p>
Each entry is the most keys you held at once, added when you release
them all.
</p>
<p role="status">
{`Recorded ${history.length} ${history.length === 1 ? 'combination' : 'combinations'}`}
</p>
<ol class="history-list">
{#each history as combination, i (i)}
<li>{combination}</li>
{/each}
</ol>
<button onclick={() => (history = [])}>Clear history</button>
</section>
</main>
</div>