Some apps hide shortcut badges until the user holds a modifier key, which keeps buttons clean while still making shortcuts discoverable on demand.
useHotkeyHint reports whether the user holds modifiers that belong to a hotkey that can fire. It's an optional display helper and doesn't register a shortcut itself, so pair it with useHotkey.
<script setup lang="ts">
import {
useFormatForDisplay,
useHotkey,
useHotkeyHint,
} from '@tanstack/vue-hotkeys'
const saveHotkey = 'Mod+S'
const formatForDisplay = useFormatForDisplay()
useHotkey(saveHotkey, save)
const showHint = useHotkeyHint(saveHotkey)
</script>
<template>
<button @click="save">
Save <kbd v-if="showHint">{{ formatForDisplay(saveHotkey) }}</kbd>
</button>
</template>While the user holds Command on macOS, or Control elsewhere, the button reads "Save ⌘ S" or "Save Ctrl+S". Formatting & Display covers labels and keycaps.
To show a hint only while every modifier is held, pass exact:
const showHint = useHotkeyHint('Alt+Shift+[KeyK]', { exact: true }) // Needs Alt and ShiftIf the hotkey sets platform, pass the same platform to useHotkeyHint, so Mod resolves the same way. The hotkey and the options can also be refs or getters.
A sequence shows its hint while the user holds the modifiers of its first step:
const showHint = useHotkeyHint(['Mod+K', 'Mod+C']) // true while Mod is heldPass an app hotkey directly so hints stay in sync when users rebind shortcuts. An app hotkey with no hotkey assigned shows no hint. A small shared component keeps every button's hint consistent:
<!-- Hint.vue -->
<script setup lang="ts">
import { computed } from 'vue'
import {
useAppHotkeys,
useFormatForDisplay,
useHotkeyHint,
} from '@tanstack/vue-hotkeys'
import type { AppHotkeyId } from '@tanstack/vue-hotkeys'
const props = defineProps<{ id: AppHotkeyId }>()
const formatForDisplay = useFormatForDisplay()
const appHotkeys = useAppHotkeys()
const appHotkey = computed(() => appHotkeys.value[props.id])
const showHint = useHotkeyHint(appHotkey)
</script>
<template>
<kbd v-if="showHint && appHotkey.hotkey">
{{ formatForDisplay(appHotkey.hotkey) }}
</kbd>
</template><script setup lang="ts">
import { useAppHotkeys, useHotkey } from '@tanstack/vue-hotkeys'
import Hint from './Hint.vue'
const appHotkeys = useAppHotkeys()
useHotkey(() => appHotkeys.value['doc.save'], save)
</script>
<template>
<button @click="save">Save <Hint id="doc.save" /></button>
</template>Outside components, such as in a canvas draw loop, combine matchesHeldModifiers with getKeyStateTracker() and getHotkeyManager().hasActiveRegistration():
import {
getHotkeyManager,
getKeyStateTracker,
matchesHeldModifiers,
} from '@tanstack/vue-hotkeys'
const tracker = getKeyStateTracker()
matchesHeldModifiers('Mod+S', tracker.getHeldKeys()) // true while Mod is held
const showHint =
matchesHeldModifiers('Mod+S', tracker.getHeldKeys()) &&
getHotkeyManager().hasActiveRegistration('Mod+S') // Same check as the composableSee Held Keys for more on getKeyStateTracker, and the Kitchen Sink example for hints on live navigation links and buttons.