<script setup lang="ts">
import { TanStackDevtools } from '@tanstack/vue-devtools'
import {
defineHotkey,
useFormatForDisplay,
useHotkeyRegistrations,
useHotkeys,
} from '@tanstack/vue-hotkeys'
import { HotkeysDevtoolsPanel } from '@tanstack/vue-hotkeys-devtools'
import { ref } from 'vue'
const plugins = [{ name: 'TanStack Hotkeys', component: HotkeysDevtoolsPanel }]
const formatForDisplay = useFormatForDisplay()
const lastCommand = ref('none')
const vimEnabled = ref(true)
const arrows = ref({ konami: 0, side: 0, steps: 0 })
useHotkeys(
[
{
hotkey: ['G', 'G'],
callback: () => (lastCommand.value = 'Go to top'),
options: { meta: { name: 'Go to top' } },
},
{
hotkey: 'Shift+G',
callback: () => (lastCommand.value = 'Go to bottom'),
options: { meta: { name: 'Go to bottom' } },
},
{
hotkey: ['D', 'D'],
callback: () => (lastCommand.value = 'Delete line'),
options: { meta: { name: 'Delete line' } },
},
{
hotkey: ['D', 'W'],
callback: () => (lastCommand.value = 'Delete word'),
options: { meta: { name: 'Delete word' } },
},
{
hotkey: ['C', 'I', 'W'],
callback: () => (lastCommand.value = 'Change inner word'),
options: { meta: { name: 'Change inner word' } },
},
],
{ timeout: 1500, enabled: vimEnabled },
)
const konami = defineHotkey({
hotkey: ['ArrowUp', 'ArrowUp', 'ArrowDown', 'ArrowDown'],
callback: (_event, context) => {
arrows.value.konami++
arrows.value.steps = context.parsedSequence.length
},
options: { timeout: 2000, meta: { name: 'Konami code' } },
})
useHotkeys([
konami,
{
hotkey: ['ArrowLeft', 'ArrowRight', 'ArrowLeft', 'ArrowRight'],
callback: () => arrows.value.side++,
options: { meta: { name: 'Side to side' } },
},
])
const registrations = useHotkeyRegistrations()
</script>
<template>
<div class="app">
<header>
<h1>Sequences with useHotkeys</h1>
<p>
Register several sequences, such as Vim commands, in one useHotkeys
call. Each key must follow the previous one within the timeout.
</p>
</header>
<main>
<section class="demo-section">
<h2>Vim-style commands</h2>
<label>
<input v-model="vimEnabled" type="checkbox" />
Enable Vim commands
</label>
<p>
Press <kbd>{{ formatForDisplay(['G', 'G']) }}</kbd>,
<kbd>{{ formatForDisplay('Shift+G') }}</kbd>,
<kbd>{{ formatForDisplay(['D', 'D']) }}</kbd>,
<kbd>{{ formatForDisplay(['D', 'W']) }}</kbd>, or
<kbd>{{ formatForDisplay(['C', 'I', 'W']) }}</kbd>, with up to 1.5
seconds between keys.
</p>
<p role="status">Last command: {{ lastCommand }}</p>
</section>
<section class="demo-section">
<h2>Check one definition with defineHotkey</h2>
<p>
Press
<kbd>{{
formatForDisplay(['ArrowUp', 'ArrowUp', 'ArrowDown', 'ArrowDown'])
}}</kbd>
or
<kbd>{{
formatForDisplay([
'ArrowLeft',
'ArrowRight',
'ArrowLeft',
'ArrowRight',
])
}}</kbd
>.
</p>
<p role="status">
Konami code: {{ arrows.konami
}}{{ arrows.steps > 0 ? ` (${arrows.steps} steps)` : '' }}, Side to
side: {{ arrows.side }}
</p>
</section>
<section class="demo-section">
<h2>List the registrations</h2>
<p role="status">{{ registrations.length }} hotkeys registered</p>
<table class="registrations-table">
<thead>
<tr>
<th>Hotkey</th>
<th>Name</th>
<th>Progress</th>
<th>Fired</th>
</tr>
</thead>
<tbody>
<tr v-for="reg in registrations" :key="reg.id">
<td>
<kbd>{{ formatForDisplay(reg.hotkey) }}</kbd>
</td>
<td>{{ reg.options.meta?.name }}</td>
<td>
{{
reg.kind === 'sequence'
? `${reg.matchedStepCount} of ${reg.hotkey.length}`
: 'chord'
}}
</td>
<td>{{ reg.triggerCount }}</td>
</tr>
</tbody>
</table>
</section>
</main>
<TanStackDevtools :plugins="plugins" />
</div>
</template>