An Angular module that provides a declarative API using components/directive to manage Keyboard shortcuts in scalable way.
This documentation is for version 7.0.0+ (8,9) and any future versions. For older versions (2.0.0/6.0.0) please click here
See demo here:
demo
We recommend to update to version 7.0.0 and above and use the new component API which has a better memory management than previous version.
Compatible with Angular 5+
npm install --save ng-keyboard-shortcuts
yarn add ng-keyboard-shortcuts
key combinations are used with meta keys like control, shift, command, etc... and are defined using plus(+) sign as a separator. there can be multiple combinations for the same command, so either of the key combination will trigger the callback. Since combinations uses the plus sign as a seperator, if you want to bind to the actual + charachater, you will need to use "plus" instead.
Sequences can be used to support gmail like actions where you click "g" then "a", or "g" then "o" to perform certain actions.
The library can get very confused if you have a single key handler that uses the same key that a sequence starts with. This is because it can't tell if you are starting the sequence or if you are pressing that key on its own.
To counter this, there is a 500ms delay(only when single key is used in the beginning of another sequence, so it won't affect performance) . This gives the library time to wait for a more complete sequence, otherwise the single sequence will be triggered.
for example: binding both "? a" and "?", then clicking "?" will trigger the "?" callback, but only after 500ms delay. However, in all other cases there's no delay in execution of the callback (unless debounceTime is provided)
This library supports gmail style sequences:
konami code:
Sequences can be used inside components or directives and are declared without the plus(+) sign, for example:
key: ["a b c"]
will require clicking, a, then b, then c.
Component that can be used across the app to bind to various shortcuts
Name | Type | Default | Description |
---|---|---|---|
shortcuts | ShortcutInput / ShortcutInput[] |
[] | List of shortcut inputs types see types |
disabled | boolean |
false |
disable the shortcuts for the component |
Name | Input | Return | Description |
---|---|---|---|
select | string - key to listen to events (example: 'cmd + e' ) |
Observable<ShortcutEventOutput> |
Listen to specific key events (will only work for registered keys) |
@angular/animations
( npm install --save @angular/animations
or yarn add @angular/animations
Can be used to show an help screen ( will be attached to body and be shown as a modal)
Should be placed in the root of your app, preferably in app.component.html
Name | Type | default | description |
---|---|---|---|
key | string |
none | The key to show/hide the help modal |
keyDescription | string |
none | Description to show in the menu shortcut list for the toggle shortcut |
keyLabel | string |
none | Label that can be used to group shortcuts together in the help menu |
closeKey | string |
none | Close key to be used to close the modal |
closeKeyDescription | string |
none | Description to show in the menu shortcut list for closing the modal shortcut |
closeKeyLabel | string |
none | Label that can be used to group shortcuts together in the help menu |
title | string |
"Keyboard shortcuts" | The title of the help screen |
emptyMessage | string |
"No shortcuts available" | What message to show when no commands are registered when help modal is opened. |
disableScrolling | boolean |
true | Whether to disable body scrolling when modal help screen is opened. |
Name | Input | Return | Description |
---|---|---|---|
hide | void | KeyboardShortcutsHelpComponent |
Programmatically hide the modal |
reveal | void | KeyboardShortcutsHelpComponent |
Programmatically hide the modal |
visible | void | boolean |
Check whether the modal is visible or not. |
toggle | void | KeyboardShortcutsHelpComponent |
Programmatically toggle the modal visibility |
Name | Input | Return | Description |
---|---|---|---|
select | string - key to listen to events (example: 'cmd + e' ) |
Observable<ShortcutEventOutput> |
Listen to specific key events (will only work for registered keys) |
app.component.ts
app.component.html
Directive that can only be used for focusable elements, such as textarea, select, input, etc...
Name | Type | default | description |
---|---|---|---|
ngKeyboardShortcut | Shortcut / Shortcut |
[] | List of shortcuts see types |
disabled | boolean |
false |
disable the shortcuts for the directive |
disableScrolling | boolean |
true |
disable body scrolling while modal is open |
Singleton service that can be used to render a custom help screen. (used to build the Built in help component)
Provides access to all registered shortcuts in the app using Observable that updates on shortcuts changes.
Since shortcuts can be added or removed during the lifecycle of the app, an observable data structure needed to be used.
properties | type | description |
---|---|---|
shortcuts$ | Observable<{ key: string, label: string, description: string }[]> |
Array of registered shortcuts across the whole app |
A singleton service that can be used globally to listen to any registered shortcut:
| Name | Input | Return | Description |
|----------|:------|:------:|:-------------:|
| select | string
- key to listen to events (example: 'cmd + e'
) | Observable<ShortcutEventOutput>
|Listen to specific key events (will only work for registered keys) |
Used for Directive input
Used for the component as input.
npm run build-lib
npm run build-lib
npm publish dist/ng-keyboard-shortcuts
This project is licensed under the MIT License - see the LICENSE.md file for details