TanStack Hotkeys provides the createHotkeyRecorder function for building shortcut customization UIs in Svelte.
<script lang="ts">
import {
createHotkeyRecorder,
formatForDisplay,
} from '@tanstack/svelte-hotkeys'
const recorder = createHotkeyRecorder({
onRecord: (hotkey) => {
console.log('Recorded:', hotkey)
},
})
</script>
<div>
<button onclick={recorder.startRecording}>
{recorder.isRecording
? 'Press a key combination...'
: recorder.recordedHotkey
? formatForDisplay(recorder.recordedHotkey)
: 'Click to record'}
</button>
{#if recorder.isRecording}
<button onclick={() => recorder.cancelRecording()}>Cancel</button>
{/if}
</div>
createHotkeyRecorder({
onRecord: (hotkey) => {},
onCancel: () => {},
onClear: () => {},
})
Options can also be reactive:
<script lang="ts">
import { createHotkeyRecorder } from '@tanstack/svelte-hotkeys'
let actionName = $state('Save')
const recorder = createHotkeyRecorder(() => ({
onRecord: (hotkey) => {
console.log(`${actionName}:`, hotkey)
},
}))
</script>