Skip to content

Angular Example: InjectHotkey Sequences

import { Component, computed, signal } from '@angular/core'
import {
  getHotkeyManager,
  injectFormatForDisplay,
  injectHotkey,
  injectHotkeyRegistrations,
} from '@tanstack/angular-hotkeys'

@Component({
  selector: 'app-root',
  standalone: true,
  templateUrl: './app.component.html',
})
export class AppComponent {
  protected readonly formatForDisplay = injectFormatForDisplay()
  protected readonly lastCommand = signal('none')
  protected readonly konamiCount = signal(0)
  protected readonly helloEnabled = signal(true)
  protected readonly helloCount = signal(0)
  protected readonly saveCount = signal(0)
  protected readonly renameCount = signal(0)
  protected readonly runTestsCount = signal(0)

  // Every sequence's progress, from its registration. Show the one with the
  // most steps matched. matchedStepCount is 0 when a sequence is idle, and
  // resets on completion, a wrong key, or a timeout.
  private readonly registrations = injectHotkeyRegistrations()
  protected readonly pendingKeys = computed(() => {
    const pending = this.registrations()
      .filter((reg) => reg.kind === 'sequence')
      .sort((a, b) => b.matchedStepCount - a.matchedStepCount)
      .at(0)
    return pending?.matchedStepCount
      ? this.formatForDisplay(pending.hotkey.slice(0, pending.matchedStepCount))
      : 'none'
  })

  constructor() {
    // An array is a sequence: press each step in order, within 1 second of the
    // step before it. `['D', 'D']` and `['D', 'W']` share a first step, and the
    // second key decides which one fires.
    injectHotkey(['G', 'G'], () => this.lastCommand.set('Go to top'))
    // A one-step array is a plain chord, the same as `'Shift+G'`.
    injectHotkey(['Shift+G'], () => this.lastCommand.set('Go to bottom'))
    injectHotkey(['D', 'D'], () => this.lastCommand.set('Delete line'))
    injectHotkey(['Y', 'Y'], () => this.lastCommand.set('Yank line'))
    injectHotkey(['D', 'W'], () => this.lastCommand.set('Delete word'))
    injectHotkey(['C', 'I', 'W'], () =>
      this.lastCommand.set('Change inner word'),
    )

    // Sequences only: allow 1.5 seconds between steps instead of the default
    // 1 second. A longer gap clears the progress, and the next ArrowUp starts
    // over.
    injectHotkey(
      ['ArrowUp', 'ArrowUp', 'ArrowDown', 'ArrowDown'],
      () => this.konamiCount.update((c) => c + 1),
      { timeout: 1500 },
    )

    // Options passed as a function follow the signals they read. While
    // `enabled` is false, the sequence stays registered but doesn't advance
    // or fire, and H E L L O reach the page as plain keys. A sequence of
    // letters counts as a character-key shortcut, so give users a way to
    // turn it off.
    injectHotkey(
      ['H', 'E', 'L', 'L', 'O'],
      () => this.helloCount.update((c) => c + 1),
      () => ({ enabled: this.helloEnabled() }),
    )

    // A 5 second timeout leaves time to click Reset progress between the two
    // presses of Z.
    injectHotkey(['Z', 'Z'], () => this.saveCount.update((c) => c + 1), {
      timeout: 5000,
    })

    // By default, a sequence whose first step types a character, such as
    // `['G', 'G']`, doesn't advance while focus is in a text field.
    // A sequence that starts with F2 still fires there, and the R that
    // finishes it doesn't type into the field.
    injectHotkey(['F2', 'R'], () => this.renameCount.update((c) => c + 1))

    // Each step is a chord. Shift can stay held between steps, and a Shift
    // press on its own doesn't reset progress. `[KeyR]` and `[KeyT]` name
    // physical keys, so the sequence fires on those positions on any layout.
    injectHotkey(['Shift+[KeyR]', 'Shift+[KeyT]'], () =>
      this.runTestsCount.update((c) => c + 1),
    )
  }

  // A click isn't a key press, so it can't cancel a sequence the way a wrong
  // key does. resetSequenceProgress clears every sequence in progress.
  protected resetProgress() {
    getHotkeyManager().resetSequenceProgress()
  }
}