Skip to main content

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
ngKeyboardShortcutsShortcut / Shortcut[]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;

}