# WrHotkey

> Global keybinding registry. Specs like `mod+k` — `mod` auto-resolves to Cmd on macOS / Ctrl elsewhere. Skips bindings while an input / textarea has focus by default. `[wrHotkey]` is the declarative form of the same registry — also global unless you pass `[scoped]="true"` to limit it to the host element.

Source: https://ngwr.dev/reference/services/hotkey  
Kind: Service

## Usage

```angular-ts
private readonly hotkeys = inject(WrHotkey);

ngOnInit() {
  const handle = this.hotkeys.bind('mod+k', () => this.openPalette(), {
    preventDefault: true,
    allowInInput: false,
  });
  this.destroyRef.onDestroy(() => handle.unbind());
}

// Or use the directive form:
// <button [wrHotkey]="'mod+k'" (wrHotkeyMatch)="openPalette()">…</button>
```

## Why ngwr provides this

Raw `keydown` listeners need platform-aware modifier mapping (⌘ vs Ctrl), input-field exclusion, and teardown. `WrHotkey` registers a declarative combo and cleans up with the host's `DestroyRef`.

## API

| Name | Description | Type | Default |
| --- | --- | --- | --- |
| `bind(spec, handler, options?)` | Register a binding. Returns `{ unbind }`. | `(spec, handler, options?) => WrHotkeyHandle` | `—` |
| `[wrHotkey]` | Directive form: binds on init, re-binds on input change, unbinds on destroy. | `WrHotkeySpec` | `—` |
| `spec format` | `'+'`-separated tokens. `mod` = Cmd on macOS, Ctrl elsewhere. | `string` | `—` |

## [wrHotkey] inputs

The directive form, bound on the element that owns the shortcut.

| Name | Description | Type | Default |
| --- | --- | --- | --- |
| `wrHotkey`required | — | `WrHotkeySpec` | — |
| `scoped` | Scope the binding to the host element instead of `document`. | `boolean` | `false` |
| `allowInInput` | Fire even when an input / textarea has focus. | `boolean` | `false` |
| `preventDefault` | Suppress the default action when the binding fires. | `boolean` | `true` |
| `(wrHotkeyMatch)` | Emits the original KeyboardEvent on match. | `KeyboardEvent` | — |

## See also

- [Keyboard](https://ngwr.dev/guides/keyboard) — How chords, keycaps and key primitives fit together in one task.
- [WrKbd](https://ngwr.dev/reference/components/keyboard) — Render the chord you just bound so users can discover it.
- [KEYS](https://ngwr.dev/reference/utils/keys) — Canonical `KeyboardEvent.key` constants for your own handlers.
