TanStack Hotkeys provides three Angular APIs for tracking live keyboard state: injectHeldKeys, injectHeldKeyCodes, and injectKeyHold.
import { Component } from '@angular/core'
import { injectHeldKeys } from '@tanstack/angular-hotkeys'
@Component({
standalone: true,
template: `
<div>
{{ heldKeys().length > 0 ? heldKeys().join(' + ') : 'No keys held' }}
</div>
`,
})
export class KeyDisplayComponent {
readonly heldKeys = injectHeldKeys()
}
import { injectHeldKeyCodes } from '@tanstack/angular-hotkeys'
readonly heldCodes = injectHeldKeyCodes()
import { injectKeyHold } from '@tanstack/angular-hotkeys'
readonly isShiftHeld = injectKeyHold('Shift')
import { Component } from '@angular/core'
import { injectKeyHold } from '@tanstack/angular-hotkeys'
@Component({
standalone: true,
template: `
@if (isShiftHeld()) {
<button>Permanently Delete</button>
} @else {
<button>Move to Trash</button>
}
`,
})
export class FileActionsComponent {
readonly isShiftHeld = injectKeyHold('Shift')
}
import { Component } from '@angular/core'
import {
formatKeyForDebuggingDisplay,
injectHeldKeyCodes,
injectHeldKeys,
} from '@tanstack/angular-hotkeys'
@Component({ standalone: true, template: `` })
export class KeyDebuggerComponent {
readonly heldKeys = injectHeldKeys()
readonly heldCodes = injectHeldKeyCodes()
readonly formatKey = formatKeyForDebuggingDisplay
}
All three APIs subscribe to the singleton KeyStateTracker:
import { getKeyStateTracker } from '@tanstack/angular-hotkeys'
const tracker = getKeyStateTracker()
tracker.getHeldKeys()
tracker.isKeyHeld('Shift')