SkilVaultSign inStart free

Catalogue / tanstack-angular-hotkeys

tanstack-angular-hotkeys

MITSignedUse when adding or fixing keyboard shortcuts in an Angular app with @tanstack/angular-hotkeys: injectHotkey and injectHotkeys in an injection context, signal-driven options, viewChild targets, provideHotkeys defaults and component scopes, app hotkey injectors, recorders, held-key and hint injectors,…
---
name: tanstack-angular-hotkeys
description: "Use when adding or fixing keyboard shortcuts in an Angular app with @tanstack/angular-hotkeys: injectHotkey and injectHotkeys in an injection context, signal-driven options, viewChild targets, provideHotkeys defaults and component scopes, app hotkey injectors, recorders, held-key and hint injectors, or replacing @HostListener keydown handlers."
license: "MIT"
metadata:
  internal: true
  tanstack-framework: "angular"
  tanstack-library: "@tanstack/angular-hotkeys"
  tanstack-library-version: "0.15.0"
  tanstack-package: "@tanstack/angular-hotkeys"
  tanstack-package-version: "0.27.0"
  tanstack-purpose: "Angular bindings for TanStack Hotkeys: inject functions, signal-driven options, provideHotkeys scopes, and the Angular-specific mistakes to avoid."
  tanstack-requires: "[\"tanstack-hotkeys-core\"]"
  tanstack-source-skill: "angular-hotkeys"
  tanstack-sources: "[\"src/injectHotkey.ts\",\"src/injectHotkeys.ts\",\"src/hotkeys-provider.ts\",\"src/injectAppHotkeys.ts\",\"src/injectHotkeyRecorder.ts\",\"TanStack/hotkeys:docs/framework/angular/quick-start.md\",\"TanStack/hotkeys:docs/framework/angular/guides/provide-hotkeys-and-scopes.md\"]"
  tanstack-type: "framework"
---

TanStack Hotkeys in Angular

Before starting, load the @tanstack/hotkeys#hotkeys-core skill (npx @tanstack/intent@latest load @tanstack/hotkeys#hotkeys-core).

import { Component, ElementRef, signal, viewChild } from '@angular/core'
import { injectHotkey } from '@tanstack/angular-hotkeys'

@Component({
  selector: 'app-panel',
  template: `<div #panel tabindex="0" (input)="isDirty.set(true)">Panel</div>`,
})
export class PanelComponent {
  readonly isDirty = signal(false)
  private readonly panel = viewChild<ElementRef<HTMLDivElement>>('panel')

  constructor() {
    injectHotkey(
      'Mod+S',
      () => this.save(),
      () => ({ enabled: this.isDirty() }),
    )
    // A viewChild signal target registers once the element renders.
    injectHotkey('Escape', () => this.close(), { target: this.panel })
  }

  save() {}
  close() {}
}

API

| API | What it's for | | -------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | injectHotkey(hotkey, callback, options?) | Register one chord, sequence, or app hotkey for the component's or service's lifetime; options can be a function that reads signals. | | injectHotkeys(definitions, commonOptions?) | Register several hotkeys, or a function returning a list built from data, in one call. | | provideHotkeys(options) | Set default options, a scope (exclusive for dialogs), enabled, suspendCharacterKeys (set from a user setting that turns off single-key shortcuts; it applies app-wide once the provider is created, so set it on the application's provideHotkeys), and app hotkeys in the app config or a component's providers. | | injectHotkeysContext() | Read the nearest provided defaults and enabled state. Activates that provider's scope. | | injectDefaultHotkeysOptions() | Read the default options merged from every provider above. | | injectFormatForDisplay() | Get the function that formats a hotkey as a label, with the provider's platform and the user's keyboard layout filled in. Only Chromium browsers report a layout, so physical keys keep their US labels in Firefox and Safari. Use it for every label in a component: readonly formatForDisplay = injectFormatForDisplay() as a field, then formatForDisplay(hotkey) in the template. | | injectFormatForAria() | Get the function that formats a hotkey for aria-keyshortcuts, with the provider's platform and the user's keyboard layout filled in. Returns undefined for a multi-step sequence, no hotkey, or a hotkey that can't fire right now, such as a disabled or suspended one, so the attribute is omitted. Set aria-keyshortcuts on every control that has a shortcut, next to the visible label. | | injectAppHotkeys() | Get a signal of the declared app hotkeys with the user's custom hotkeys applied. | | injectAppHotkeyList() | Get a signal listing every app hotkey with its status, for a cheat sheet or settings screen. | | injectCustomHotkeys() | Read, change, and reset the user's custom hotkeys, and check a new one for conflicts. | | injectHotkeyRecorder(options) | Record a chord or sequence the user presses, for a settings screen. | | injectHotkeyRegistrations() | Get a signal listing every registered hotkey with its options, meta, and status (whether it's enabled and in an active scope). | | injectHotkeyScopesState() | Get a signal of the active scopes and the exclusive scope that owns the keyboard. | | injectHotkeyHint(hotkey, options?) | Get a signal of whether held modifiers should reveal a shortcut's hint. False unless a registration of the hotkey can fire: enabled, in an active scope, and with focus inside its element target. | | injectKeyHold(key) | Get a signal of whether one key is held. Letters match in any case; other names, such as Shift, are case-sensitive. | | injectKeyLockState(key) | Get a signal of whether 'CapsLock', 'NumLock', or 'ScrollLock' is on: true, false, or null until a key press, click, or mouse move reports it. Resets to null when the window loses focus. Num Lock and Scroll Lock are always null on macOS and iOS. Use it for lock state, not injectKeyHold, which is true only while the key is down. | | injectHeldKeys() | Get a signal of every key that's held right now. | | injectHeldKeyCodes() | Get a signal mapping each held key to its physical event.code. |

The package also re-exports @tanstack/hotkeys, such as matchesKeyboardEvent and defineAppHotkeys. Its plain formatForDisplay export is deprecated: use injectFormatForDisplay() in an injection context. Outside an injection context, call the plain function and pass platform. The plain formatForAria export is deprecated the same way, in favor of injectFormatForAria().

No provider is required. Call the inject functions in a constructor or field initializer.

A dialog that owns the keyboard

import { Component, InjectionToken, inject, output } from '@angular/core'
import { injectHotkey, provideHotkeys } from '@tanstack/angular-hotkeys'

let nextDialogId = 0
const DIALOG_SCOPE = new InjectionToken<string>('DIALOG_SCOPE')

@Component({
  selector: 'app-dialog',
  providers: [
    { provide: DIALOG_SCOPE, useFactory: () => `dialog-${++nextDialogId}` },
    provideHotkeys({ scope: () => inject(DIALOG_SCOPE), exclusive: true }),
  ],
  template: `<div role="dialog"><ng-content /></div>`,
})
export class DialogComponent {
  readonly closed = output()

Preview shortened. The full SKILL.md is in the pack.