@molecule/app-keyboard-shortcuts-angular
Feature · keyboard-shortcuts · App (browser) · v1.1.0 · Apache-2.0
Angular functions for @molecule/app-keyboard-shortcuts — register a shortcut for the life of a component
npm install @molecule/app-keyboard-shortcuts-angularHow it works
@molecule/app-keyboard-shortcuts-angular is a ready-made keyboard-shortcuts feature for the app (browser) side. It composes the core interfaces it needs, so it works with whichever providers your app has bonded.
import { Component, type ElementRef, viewChild } from '@angular/core'
import { useTranslation } from '@molecule/app-angular'
import { useKeyboardShortcut } from '@molecule/app-keyboard-shortcuts-angular'
@Component({
selector: 'app-search',
template: '<input #input />',
})
export class Search {
// The binding's t re-runs the template on a locale switch.
protected readonly t = useTranslation().t
readonly input = viewChild.required<ElementRef<HTMLInputElement>>('input')
// A field initializer runs in the injection context.
readonly shortcuts = useKeyboardShortcut('/', () => this.input().nativeElement.focus(), {
description: this.t('catalogSearch.focus', undefined, { defaultValue: 'Focus search' }),
})
readonly palette = useKeyboardShortcut('mod+k', () => openPalette())
}Works with: @molecule/app-angular, @molecule/app-keyboard-shortcuts, @molecule/app-ui, @molecule/app-ui-angular
Reference
Angular functions for @molecule/app-keyboard-shortcuts.
useKeyboardShortcut registers a shortcut with the bonded provider
for the life of a component and always calls the latest handler, so a
component can bind / to focus its search box or mod+k to open a
palette in one line. It is the Angular counterpart of
@molecule/app-keyboard-shortcuts-react: same names, same options, same
provider calls.
Auto-generated, AI-first package reference for the molecule.dev ecosystem. It is written to be read by coding agents as much as by people, and is generated from this package's source — edit
src/index.tsJSDoc, not this file.
Quick Start
import { Component, type ElementRef, viewChild } from '@angular/core'
import { useTranslation } from '@molecule/app-angular'
import { useKeyboardShortcut } from '@molecule/app-keyboard-shortcuts-angular'
@Component({
selector: 'app-search',
template: '<input #input />',
})
export class Search {
// The binding's t re-runs the template on a locale switch.
protected readonly t = useTranslation().t
readonly input = viewChild.required<ElementRef<HTMLInputElement>>('input')
// A field initializer runs in the injection context.
readonly shortcuts = useKeyboardShortcut('/', () => this.input().nativeElement.focus(), {
description: this.t('catalogSearch.focus', undefined, { defaultValue: 'Focus search' }),
})
readonly palette = useKeyboardShortcut('mod+k', () => openPalette())
}
Type
feature
Installation
npm install @molecule/app-keyboard-shortcuts-angular @angular/common @angular/core @angular/platform-browser @molecule/app-angular @molecule/app-keyboard-shortcuts @molecule/app-ui @molecule/app-ui-angular
API
Interfaces
UseKeyboardShortcutOptions
Options for useKeyboardShortcut in @molecule/app-keyboard-shortcuts-angular.
interface UseKeyboardShortcutOptions {
/** Register the shortcut at all. `false` unregisters it. Defaults to `true`. */
enabled?: boolean
/** What the shortcut does, already translated, for a shortcuts panel. */
description?: string
/** The provider scope to register under. */
scope?: string
/** Call `preventDefault()` on the event. Defaults to `true`. */
preventDefault?: boolean
}
Functions
expandKeys(keys)
Normalizes a key spec: 'mod+k' becomes both ctrl+k and command+k, so
one declaration serves every platform.
function expandKeys(keys: string | string[]): string[]
keys— One combination or several.
Returns: The combinations to register.
useKeyboardShortcut(keys, handler, options)
Registers keys with the bonded keyboard-shortcuts provider for the life
of the component (DestroyRef), and unregisters on destroy or when
enabled turns false. The latest handler is always the one called: pass a
changing action as a signal, which the registration reads at call time.
With no provider bonded the function does nothing.
Call it in an injection context (a component field initializer or
constructor), the way an app calls Angular's injection-based APIs. Pass
keys or options as a signal when any part of them can change: a changed
combination, enable flag, description, scope or preventDefault flag
unregisters the previous registration and registers the new one on the SAME
instance, the way React re-renders the hook with new arguments.
function useKeyboardShortcut(
keys: string | string[] | Signal<string | string[]>,
handler: ShortcutHandler | Signal<ShortcutHandler>,
options?: UseKeyboardShortcutOptions | Signal<UseKeyboardShortcutOptions>,
): void
keys— One combination or several ('mod+k'is expanded toctrl+kandcommand+k), or a signal of either.handler— Called with the keyboard event; or a signal of one, read at call time.options— Enable flag, description, scope, preventDefault; or a signal of them.
Injection Notes
Requirements
Peer dependencies:
@angular/common^22.0.0@angular/core^22.0.0@angular/platform-browser^22.0.0@molecule/app-angular^3.5.0@molecule/app-keyboard-shortcuts^1.0.1@molecule/app-ui^1.0.1@molecule/app-ui-angular^1.1.0
Runtime Dependencies
-
@angular/common -
@angular/core -
@angular/platform-browser -
@molecule/app-angular -
@molecule/app-keyboard-shortcuts -
@molecule/app-ui -
@molecule/app-ui-angular -
mod+means Ctrl on Windows and Linux, Command on macOS — the function registers both, so never writectrl+kfor a shortcut people expect as⌘Kon a Mac. -
Whether a shortcut fires while an input has focus is the bond's decision (the hotkeys bond suppresses it, which is what
/needs). Keys the input itself should handle — Escape to clear, arrows to move — belong on the input's own keydown handling, not here. -
With no provider bonded the function is a no-op rather than an error, so a shared component still renders in an app that never wired shortcuts.
-
The
descriptionis shown by shortcut panels: pass it throught(). -
Call it in an injection context (a field initializer or constructor). Pass
keysoroptionsas a signal when any part of them can change, and a changing handler as a signal too; both are read at call time.