13.0.8 • Published 2 years ago

ng-keyboard-shortcuts v13.0.8

Weekly downloads
4,502
License
MIT
Repository
-
Last release
2 years ago

ng-keyboard-shortcuts

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

important note

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+

Install:

NPM
Angular latest

npm install --save ng-keyboard-shortcuts

Angular 8/9

npm install --save ng-keyboard-shortcuts@^9.0.0

Angular 7

npm install --save ng-keyboard-shortcuts@^7.0.0

Yarn
Angular latest

yarn add ng-keyboard-shortcuts

Angular 8/9

npm add ng-keyboard-shortcuts@^9.0.0

Angular 7

yarn add ng-keyboard-shortcuts@^7.0.0

Setup:

import { KeyboardShortcutsModule }     from 'ng-keyboard-shortcuts';  
  
@NgModule({  
    declarations: [  
    ],  
    imports: [  
        BrowserModule,  
        KeyboardShortcutsModule.forRoot()  
    ],  
    bootstrap: [AppComponent]  
})  
export class AppModule {  
}  

Usage:

Combinations

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.

Examples:

   [
       {
            key: ["cmd + a"],
            command: (output: ShortcutEventOutput) => console.log("command + a", output),
        },
        {
            key: "ctrl + a",
            preventDefault: true,
            command: (output: ShortcutEventOutput) => console.log("control + a", output),
            
        },
        {
            key: "ctrl + plus",
            preventDefault: true,
            command: (output: ShortcutEventOutput) => console.log("control + plus key", output),
        }
    ]

Sequences

Sequences can be used to support gmail like actions where you click "g" then "a", or "g" then "o" to perform certain actions.

Important note

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)

Examples:

This library supports gmail style sequences:

   [{
        key: ["g a"],
        command: (output: ShortcutEventOutput) => console.log("? a", output),
    }]

konami code:

    [{
         key: ["up up down down left right left right b a enter"],
         label: "Sequences",
         description: "Konami code!",
         command: (output: ShortcutEventOutput) => console.log("Konami code!!!", output),
    }]

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.

Components:

ng-keyboard-shortcuts

Component that can be used across the app to bind to various shortcuts

Inputs:

NameTypeDefaultDescription
shortcutsbashortcutInput / bashortcutInput[][]List of shortcut inputs types see types
disabledbooleanfalsedisable the shortcuts for the component

Methods:

NameInputReturnDescription
selectstring - key to listen to events (example: 'cmd + e')Observable<ShortcutEventOutput>Listen to specific key events (will only work for registered keys)
  
import { AfterViewInit, Component, ElementRef, OnInit, ViewChild } from "@angular/core";  
import { ShortcutInput, ShortcutEventOutput, KeyboardShortcutsComponent } from "ng-keyboard-shortcuts";  
  
@Component({  
    selector: 'demo-component',  
    template: "<ng-keyboard-shortcuts [shortcuts]="shortcuts"></ng-keyboard-shortcuts>"  
})  
export class DemoComponent implements AfterViewInit {  
  
    shortcuts: ShortcutInput[] = [];  
    @ViewChild('input') input: ElementRef;  
  
    ngAfterViewInit(): void {  
        this.shortcuts.push(  
            {  
                key: "ctrl + t",  
                preventDefault: true,  
                allowIn: [AllowIn.Textarea, AllowIn.Input],  
                command: e => console.log("clicked " , e.key)  
            },
           {
                key: ["? a"],
                label: "Sequences",
                description: "Sequence ? and a",
                command: (output: ShortcutEventOutput) => console.log("? a", output),
                preventDefault: true
            },            
            {
                key: ["up up down down left right left right b a enter"],
                label: "Sequences",
                description: "Konami code!",
                command: (output: ShortcutEventOutput) => console.log("Konami code!!!", output),
            },
            {  
                key: "cmd + shift + f",  
                command: (output: ShortcutEventOutput) => console.log(output),  
                preventDefault: true,  
                throttleTime: 250,  
                target: this.input.nativeElement  
            },  
            {  
                key: ["cmd + =", "cmd + z"],  
                command: (output: ShortcutEventOutput) => console.log(output),  
                preventDefault: true  
            },  
            {  
                key: "cmd + f",  
                command: (output: ShortcutEventOutput) => console.log(output),  
                preventDefault: true  
            }  
        );  
  
        this.keyboard.select("cmd + f").subscribe(e => console.log(e));  
    }  
  
    @ViewChild(KeyboardShortcutsComponent) private keyboard: KeyboardShortcutsComponent;  
  
}  

ng-keyboard-shortcuts-help

  1. Make sure to install @angular/animations ( npm install --save @angular/animations or yarn add @angular/animations
  2. Add BrowserAnimationsModule to your app.module imports

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

Inputs

NameTypedefaultdescription
keystringnoneThe key to show/hide the help modal
keyDescriptionstringnoneDescription to show in the menu shortcut list for the toggle shortcut
keyLabelstringnoneLabel that can be used to group shortcuts together in the help menu
closeKeystringnoneClose key to be used to close the modal
closeKeyDescriptionstringnoneDescription to show in the menu shortcut list for closing the modal shortcut
closeKeyLabelstringnoneLabel that can be used to group shortcuts together in the help menu
titlestring"Keyboard shortcuts"The title of the help screen
emptyMessagestring"No shortcuts available"What message to show when no commands are registered when help modal is opened.
disableScrollingbooleantrueWhether to disable body scrolling when modal help screen is opened.

Methods:

NameInputReturnDescription
hidevoidKeyboardShortcutsHelpComponentProgrammatically hide the modal
revealvoidKeyboardShortcutsHelpComponentProgrammatically hide the modal
visiblevoidbooleanCheck whether the modal is visible or not.
togglevoidKeyboardShortcutsHelpComponentProgrammatically toggle the modal visibility

Methods:

NameInputReturnDescription
selectstring - key to listen to events (example: 'cmd + e')Observable<ShortcutEventOutput>Listen to specific key events (will only work for registered keys)

app.component.ts

import { Component } from "@angular/core";  
  
@Component({  
  selector: "app-root",  
  template: "./app.component.html",  
  styleUrls: ["./app.component.css"]  
})  
export class AppComponent {  
  title = "Hello";  
}  
  

app.component.html

<div style="text-align:center">  
   <h1>  
      Welcome to {{ title }}!  
   </h1>  
   <ng-keyboard-shortcuts-help [key]="'f1'" [closeKey]="'escape'" [title]="'Help'"></ng-keyboard-shortcuts-help>  
</div>  
  

Directive

ngKeyboardShortcuts

Directive can only be used for focusable elements, such as textarea, select, input, etc...

Important:

The shortcut then will only be active while the element is in focus.

Inputs

NameTypedefaultdescription
ngKeyboardShortcutsbashortcut / bashortcut[]List of shortcuts see types
disabledbooleanfalsedisable the shortcuts for the directive
disableScrollingbooleantruedisable body scrolling while modal is open

Example

import { AfterViewInit, Component, ElementRef, OnInit, ViewChild } from "@angular/core";  
import { ShortcutInput, ShortcutEventOutput, KeyboardShortcutsComponent } from "ng-keyboard-shortcuts";  
  
@Component({  
    selector: 'demo-component',  
    template: "<input [ngKeyboardShortcuts]="shortcuts" />"  
})  
export class DemoComponent implements AfterViewInit {  
  
    shortcuts: ShortcutInput[] = [];  
    @ViewChild('input') input: ElementRef;  
  
    ngAfterViewInit(): void {  
        this.shortcuts.push({  
            key: "cmd + e",  
            label: "test",  
            description: "hello world",  
            command: () => console.log('directive cmd + e'),  
            preventDefault: true  
        });  
  
        this.keyboard.select("cmd + f").subscribe(e => console.log(e));  
    }  
  
    @ViewChild(KeyboardShortcutsComponent) private keyboard: KeyboardShortcutsComponent;  
  
}  

Service

KeyboardShortcutsHelpService

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.

propertiestypedescription
shortcuts$Observable<{ key: string, label: string, description: string }[]>Array of registered shortcuts across the whole app

KeyboardShortcutsSelectService

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) |

API:

Types:

AllowIn

export enum AllowIn {  
    Textarea = 'TEXTAREA',  
    Input = 'INPUT',  
    Select = "SELECT"  
}  

Shortcut

Used for Directive input

export interface Shortcut {  
  
    key: string | string[];  
  
    /**  
     * callback to be called when shortcut is pressed.  
     * @param event - the event out  
     */  
    command(event: ShortcutEventOutput): any;  
  
    /**  
     * Description for the command can be used for rendering help menu.  
     */  
    description?: string;  
  
    /**  
     * How much time to throttle in ms.  
     */  
    throttleTime?: number;  
  
    /**  
     * Label, can be used for grouping commands.  
     */  
    label?: string;  
  
    /**  
     * Prevent browser default, default: false  
     */  
    preventDefault?: boolean;  
}  

ShortcutInput

Used for the component as input.

export interface ShortcutInput extends Shortcut {  
    /**  
     * textarea, select and input are ignored by default, this is used to override  
     * this behavior.  
     * allow in node names, accepts: ["TEXTAREA", "SELECT", "INPUT]  
     */  
    allowIn?: AllowIn[];  
    /**  
     * Only trigger the command when the target is in focus.  
     */  
    target?: HTMLElement;  
}  

ShortcutEventOutput

type = ShortcutEventOutput {  
    event: KeyboardEvent;  
    key: string | string[];  
}  
  

Building

npm run build-lib

Publishing

  1. npm run build-lib
  2. npm publish dist/ng-keyboard-shortcuts

License

This project is licensed under the MIT License - see the LICENSE file for details

13.0.8

2 years ago

13.0.6

2 years ago

13.0.7

2 years ago

13.0.5

2 years ago

13.0.4

2 years ago

13.0.2

2 years ago

13.0.3

2 years ago

13.0.0

2 years ago

13.0.1

2 years ago

10.1.17

3 years ago

9.1.17

3 years ago

7.1.17

3 years ago

10.1.16

4 years ago

9.1.14

4 years ago

9.1.15

4 years ago

10.1.15

4 years ago

7.1.14

4 years ago

7.1.15

4 years ago

9.1.13

4 years ago

10.1.13

4 years ago

7.1.13

4 years ago

7.1.12

4 years ago

10.1.12

4 years ago

9.1.12

4 years ago

9.1.11

4 years ago

10.1.11

4 years ago

7.1.11

4 years ago

10.1.10

4 years ago

9.1.10

4 years ago

9.1.8

4 years ago

10.1.5

4 years ago

9.1.7

4 years ago

10.1.6

4 years ago

9.1.6

4 years ago

10.1.8

4 years ago

10.1.9

4 years ago

7.1.10

4 years ago

7.1.9

4 years ago

7.1.8

4 years ago

9.1.5

4 years ago

7.1.5

4 years ago

7.1.4

4 years ago

9.0.1

4 years ago

8.2.6

4 years ago

9.0.0

4 years ago

8.2.5

4 years ago

8.2.4

4 years ago

8.2.3

4 years ago

8.2.2

4 years ago

8.2.1

4 years ago

8.2.0

4 years ago

8.0.0

5 years ago

7.1.3

5 years ago

7.1.2

5 years ago

7.1.1

5 years ago

7.1.0

5 years ago

7.0.11

5 years ago

7.0.10

5 years ago

7.0.9

5 years ago

7.0.8

5 years ago

7.0.7

5 years ago

7.0.6

5 years ago

7.0.5

5 years ago

7.0.4

5 years ago

7.0.3

5 years ago

7.0.2

5 years ago

7.0.1

5 years ago

7.0.0

5 years ago

6.1.1

6 years ago

6.1.0

6 years ago

2.0.5

6 years ago

2.0.4

6 years ago

2.0.3

6 years ago

2.0.2

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.1.6

7 years ago

1.1.5

7 years ago

1.1.3

7 years ago

1.1.2

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.23

7 years ago

1.0.21

7 years ago

1.0.20

7 years ago

1.0.18

7 years ago

1.0.17

7 years ago

1.0.16

7 years ago

1.0.15

7 years ago

1.0.14

7 years ago

1.0.13

7 years ago

1.0.12

7 years ago

1.0.11

7 years ago

1.0.10

7 years ago

1.0.0

7 years ago