Some apps hide shortcut badges until the user holds a modifier key, keeping buttons clean while still making shortcuts discoverable on demand.
useHotkeyHint returns an object whose tracked value is 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 the useHotkey helper.
import Component from '@glimmer/component'
import { on } from '@ember/modifier'
import {
useFormatForDisplay,
useHotkey,
useHotkeyHint,
} from '@tanstack/ember-hotkeys'
const saveHotkey = 'Mod+S'
export default class SaveButton extends Component {
formatForDisplay = useFormatForDisplay()
hint = useHotkeyHint(this, saveHotkey)
save = () => {
/* ... */
}
<template>
{{useHotkey saveHotkey this.save}}
<button type='button' {{on 'click' this.save}}>
Save {{#if this.hint.value}}<kbd>{{this.formatForDisplay saveHotkey}}</kbd>{{/if}}
</button>
</template>
}Holding Command on macOS or Control elsewhere reveals Save ⌘ S or Save Ctrl+S. See Formatting & Display for formatting options.
hint = useHotkeyHint(this, 'Alt+Shift+[KeyK]', { exact: true }) // Requires Alt AND ShiftSequences reveal their hint while the user holds the modifiers of their first step:
hint = useHotkeyHint(this, ['Mod+K', 'Mod+C']) // hint.value is 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 component keeps buttons tidy:
import Component from '@glimmer/component'
import {
useAppHotkeys,
useFormatForDisplay,
useHotkeyHint,
} from '@tanstack/ember-hotkeys'
import type { AppHotkeyId } from '@tanstack/ember-hotkeys'
export default class Hint extends Component<{ Args: { id: AppHotkeyId } }> {
formatForDisplay = useFormatForDisplay()
appHotkeys = useAppHotkeys(this)
hint = useHotkeyHint(this, () => this.appHotkeys.value[this.args.id])
get label() {
const { hotkey } = this.appHotkeys.value[this.args.id]
return this.hint.value && hotkey ? this.formatForDisplay(hotkey) : null
}
<template>
{{#if this.label}}<kbd>{{this.label}}</kbd>{{/if}}
</template>
}import Component from '@glimmer/component'
import { on } from '@ember/modifier'
import { useAppHotkeys, useHotkey } from '@tanstack/ember-hotkeys'
import Hint from './hint'
export default class Toolbar extends Component {
appHotkeys = useAppHotkeys(this)
save = () => {
/* ... */
}
get saveHotkey() {
return this.appHotkeys.value['doc.save']
}
<template>
{{useHotkey this.saveHotkey this.save}}
<button type='button' {{on 'click' this.save}}>Save <Hint @id='doc.save' /></button>
</template>
}Outside Ember components, such as canvas draw loops, combine matchesHeldModifiers with getKeyStateTracker() and getHotkeyManager().hasActiveRegistration():
import {
getHotkeyManager,
getKeyStateTracker,
matchesHeldModifiers,
} from '@tanstack/ember-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 utilitySee Held Keys for more on getKeyStateTracker, and check the Kitchen Sink example to see hints on live buttons.