Usage
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 |
|---|---|---|---|
wrHotkeyrequired | — | 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 | — |