Skip to content

Angular Example: InjectHotkeys Sequences

import { Component, signal } from '@angular/core'
import {
  defineHotkey,
  injectFormatForDisplay,
  injectHotkeyRegistrations,
  injectHotkeys,
} 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 vimEnabled = signal(true)
  protected readonly arrows = signal({ konami: 0, side: 0, steps: 0 })

  // injectHotkeys gives every array item one type that fits any hotkey, so an
  // item's callback `context` can be a chord or a sequence. defineHotkey types
  // one definition by its hotkey and returns it unchanged. This hotkey is a
  // sequence, so `context.parsedSequence` needs no kind check, and
  // `requireReset` would be a type error because it applies only to chords.
  private readonly konami = defineHotkey({
    hotkey: ['ArrowUp', 'ArrowUp', 'ArrowDown', 'ArrowDown'],
    callback: (_event, context) =>
      this.arrows.update((a) => ({
        ...a,
        konami: a.konami + 1,
        steps: context.parsedSequence.length,
      })),
    // Allow 2 seconds between steps instead of the default 1 second.
    options: { timeout: 2000, meta: { name: 'Konami code' } },
  })

  // Every registration on the page. Sequence views also carry
  // matchedStepCount, the steps pressed so far in the current attempt.
  protected readonly registrations = injectHotkeyRegistrations()

  constructor() {
    // Arrays are sequences, and chords can share the list. The second argument
    // holds options for every item: `timeout` reaches only the sequences, so
    // the Shift+G chord ignores it. Passed as a function, it follows the
    // `vimEnabled` signal. A sequence of letters counts as a character-key
    // shortcut, so the checkbox gives users a way to turn the list off.
    injectHotkeys(
      [
        {
          hotkey: ['G', 'G'],
          callback: () => this.lastCommand.set('Go to top'),
          options: { meta: { name: 'Go to top' } },
        },
        {
          hotkey: 'Shift+G',
          callback: () => this.lastCommand.set('Go to bottom'),
          options: { meta: { name: 'Go to bottom' } },
        },
        {
          hotkey: ['D', 'D'],
          callback: () => this.lastCommand.set('Delete line'),
          options: { meta: { name: 'Delete line' } },
        },
        {
          hotkey: ['D', 'W'],
          callback: () => this.lastCommand.set('Delete word'),
          options: { meta: { name: 'Delete word' } },
        },
        {
          hotkey: ['C', 'I', 'W'],
          callback: () => this.lastCommand.set('Change inner word'),
          options: { meta: { name: 'Change inner word' } },
        },
      ],
      () => ({ timeout: 1500, enabled: this.vimEnabled() }),
    )

    // A defined hotkey and an inline item can share one array.
    injectHotkeys([
      this.konami,
      {
        hotkey: ['ArrowLeft', 'ArrowRight', 'ArrowLeft', 'ArrowRight'],
        callback: () => this.arrows.update((a) => ({ ...a, side: a.side + 1 })),
        options: { meta: { name: 'Side to side' } },
      },
    ])
  }
}