Some apps hide shortcut badges until the user holds a modifier key, keeping buttons clean while still making shortcuts discoverable on demand.
HotkeyHintController exposes a value getter that returns true while the user holds modifiers belonging to an active hotkey. It's an optional display helper and doesn't register a shortcut itself, so pair it with @hotkey or HotkeyController.
import { LitElement, html } from 'lit'
import { customElement } from 'lit/decorators.js'
import {
FormatForDisplayController,
HotkeyHintController,
hotkey,
} from '@tanstack/lit-hotkeys'
const saveHotkey = 'Mod+S'
@customElement('save-button')
class SaveButton extends LitElement {
private hint = new HotkeyHintController(this, saveHotkey)
private formatForDisplay = new FormatForDisplayController(this)
@hotkey(saveHotkey)
save() {
/* ... */
}
render() {
return html`
<button @click=${() => this.save()}>
Save
${this.hint.value
? html`<kbd>${this.formatForDisplay.format(saveHotkey)}</kbd>`
: ''}
</button>
`
}
}The controller requests an update only when the hint appears or disappears. Holding Command on macOS or Control elsewhere reveals Save ⌘ S or Save Ctrl+S. See Formatting & Display for formatting options.
private hint = new HotkeyHintController(this, 'Alt+Shift+[KeyK]', {
exact: true,
}) // Requires Alt AND ShiftSequences reveal their hint while the user holds the modifiers of their first step:
private hint = new HotkeyHintController(this, ['Mod+K', 'Mod+C']) // true while Mod is heldPass an app hotkey directly so hints stay synchronized even when users rebind shortcuts. Unassigned app hotkeys never show a hint. A small shared element keeps buttons tidy:
import { LitElement, html } from 'lit'
import { customElement, property } from 'lit/decorators.js'
import {
AppHotkeysController,
FormatForDisplayController,
HotkeyHintController,
} from '@tanstack/lit-hotkeys'
import type { AppHotkeyId } from '@tanstack/lit-hotkeys'
@customElement('shortcut-hint')
class ShortcutHint extends LitElement {
@property() hotkeyId!: AppHotkeyId
private app = new AppHotkeysController(this)
private hint = new HotkeyHintController(
this,
() => this.app.appHotkeys[this.hotkeyId],
)
private formatForDisplay = new FormatForDisplayController(this)
render() {
const { hotkey } = this.app.appHotkeys[this.hotkeyId]
return this.hint.value && hotkey
? html`<kbd>${this.formatForDisplay.format(hotkey)}</kbd>`
: ''
}
}import { hotkey } from '@tanstack/lit-hotkeys'
import { appHotkeys } from './app-hotkeys'
@customElement('doc-toolbar')
class DocToolbar extends LitElement {
@hotkey(appHotkeys['doc.save'])
save() {
/* ... */
}
render() {
return html`<button @click=${() => this.save()}>
Save <shortcut-hint hotkeyId="doc.save"></shortcut-hint>
</button>`
}
}Outside Lit elements, such as canvas draw loops, combine matchesHeldModifiers with getKeyStateTracker() and getHotkeyManager().hasActiveRegistration():
import {
getHotkeyManager,
getKeyStateTracker,
matchesHeldModifiers,
} from '@tanstack/lit-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 controllerSee Held Keys for more on getKeyStateTracker, and check the Kitchen Sink example to see hints on live buttons.