Docs
CodeRabbit
Cloudflare
AG Grid
SerpAPI
Netlify
OpenRouter
Neon
WorkOS
Clerk
Electric
PowerSync
Sentry
Railway
Prisma
Strapi
Unkey
CodeRabbit
Cloudflare
AG Grid
SerpAPI
Netlify
OpenRouter
Neon
WorkOS
Clerk
Electric
PowerSync
Sentry
Railway
Prisma
Strapi
Unkey
Hotkeys API Reference
Hotkey Sequence API Reference
Key Hold API Reference
Held Keys API Reference
Hotkey Recorder API Reference
Hotkey Sequence Recorder API Reference
Format for Display API Reference
Hotkeys API Reference

injectHotkey

Function: injectHotkey()

ts
function injectHotkey(
   hotkey, 
   callback, 
   options): void;

Defined in: injectHotkey.ts:86

Angular inject-based API for registering a keyboard hotkey.

Uses the singleton HotkeyManager for efficient event handling. The callback receives both the keyboard event and a context object containing the hotkey string and parsed hotkey.

Call in an injection context (e.g. constructor or field initializer). Uses effect() to track reactive dependencies and update registration when options or the callback change. enabled: false keeps the registration (visible in devtools) and only suppresses firing; the same handle is updated instead of unregistering and re-registering when identity is unchanged.

Parameters

hotkey

The hotkey string (e.g. 'Mod+S', 'Escape') or getter function

RegisterableHotkey | () => RegisterableHotkey

callback

HotkeyCallback

The function to call when the hotkey is pressed

options

Options for the hotkey behavior, or getter for reactive options

InjectHotkeyOptions | () => InjectHotkeyOptions

Returns

void

Examples

ts
@Component({ ... })
export class SaveButtonComponent {
  private readonly saveCount = signal(0)

  constructor() {
    injectHotkey('Mod+S', (event, { hotkey }) => {
      event.preventDefault()
      this.saveCount.update(c => c + 1)
    })
  }
}
ts
@Component({ ... })
export class ModalComponent {
  isOpen = signal(true)

  constructor() {
    injectHotkey('Escape', () => this.close(), () => ({ enabled: this.isOpen() }))
  }
}
ts
@Component({ ... })
export class EditorComponent {
  private readonly editorRef = viewChild<ElementRef<HTMLDivElement>>('editorRef')

  constructor() {
    injectHotkey('Mod+B', () => this.toggleBold(), () => ({
      target: this.editorRef()?.nativeElement ?? null,
    }))
  }
}