Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
export enum Keys {
Backspace = 'Backspace',
Tab = 'Tab',
Enter = 'Enter',
Shift = 'Shift',
Control = 'Control',
Alt = 'Alt',
Pause = 'Pause',
CapsLock = 'CapsLock',
Escape = 'Escape',
Space = '',
PageUp = 'PageUp',
PageDown = 'PageDown',
End = 'End',
Home = 'Home',
ArrowLeft = 'ArrowLeft',
ArrowUp = 'ArrowUp',
ArrowRight = 'ArrowRight',
ArrowDown = 'ArrowDown',
PrintScreen = 'PrintScreen',
Insert = 'Insert',
Delete = 'Delete',
Digit0 = '0',
Digit1 = '1',
Digit2 = '2',
Digit3 = '3',
Digit4 = '4',
Digit5 = '5',
Digit6 = '6',
Digit7 = '7',
Digit8 = '8',
Digit9 = '9',
A = 'A',
B = 'B',
C = 'C',
D = 'D',
E = 'E',
F = 'F',
G = 'G',
H = 'H',
I = 'I',
J = 'J',
K = 'K',
L = 'L',
M = 'M',
N = 'N',
O = 'O',
P = 'P',
Q = 'Q',
R = 'R',
S = 'S',
T = 'T',
U = 'U',
V = 'V',
W = 'W',
X = 'X',
Y = 'Y',
Z = 'Z',
a = 'a',
b = 'b',
c = 'c',
d = 'd',
e = 'e',
f = 'f',
g = 'g',
h = 'h',
i = 'i',
j = 'j',
k = 'k',
l = 'l',
m = 'm',
n = 'n',
o = 'o',
p = 'p',
q = 'q',
r = 'r',
s = 's',
t = 't',
u = 'u',
v = 'v',
w = 'w',
x = 'x',
y = 'y',
z = 'z',
Meta = 'Meta',
ContextMenu = 'ContextMenu',
AudioVolumeMute = 'AudioVolumeMute',
AudioVolumeDown = 'AudioVolumeDown',
AudioVolumeUp = 'AudioVolumeUp',
F1 = 'F1',
F2 = 'F2',
F3 = 'F3',
F4 = 'F4',
F5 = 'F5',
F6 = 'F6',
F7 = 'F7',
F8 = 'F8',
F9 = 'F9',
F10 = 'F10',
F11 = 'F11',
F12 = 'F12',
NumLock = 'NumLock',
ScrollLock = 'ScrollLock',
Semicolon = ';',
Equal = '=',
Comma = ',',
Minus = '-',
Period = '.',
Slash = '/',
Backquote = '`',
BracketLeft = '[',
Backslash = '\\',
BracketRight = ']',
Quote = "'",
Tilde = '~',
Exclamation = '!',
At = '@',
Sharp = '#',
Dollar = '$',
Percent = '%',
Caret = '^',
Ampersand = '&',
Asterisk = '*',
ParenthesisLeft = '(',
ParenthesisRight = ')',
Underscore = '_',
Plus = '+',
OpenBrace = '{',
CloseBrace = '}',
Pipe = '|',
Colon = ':',
Quote2 = '"',
AngleBracketLeft = '<',
AngleBracketRight = '>',
QuestionMark = '?'

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import {DOCUMENT} from "@angular/common";
import {Inject, Injectable} from "@angular/core";
import {EventManager} from "@angular/platform-browser";
import {Observable, throwError} from "rxjs";
import {Keys} from "./keys";

type KeyboardShortcutListener = Observable<{ event: KeyboardEvent; name: string; }>;
type KeyboardShortcutListeners = Array<KeyboardShortcutListener>;

type ShortcutKey = Keys | string;
type ShortcutKeys = Array<ShortcutKey>;

type KeyboardShortcuts = Array<KeyboardShortcut>;
type KeyboardShortcut = {
name: string;
keys: ShortcutKeys;
element?: HTMLElement;
};

@Injectable({
providedIn: 'root'
})
export class ShortcutManagerService {
private _shortcuts = new Map<string, KeyboardShortcutListener>();

constructor(
private eventManager: EventManager,
@Inject(DOCUMENT) private _document: Document
) {}

registerShortcut({name, keys, element}: KeyboardShortcut): KeyboardShortcutListener {
const key = this.concatKeys(keys);
const targetElement = element ?? this._document.body;
const shortcut = this.createShortcut(name, key, targetElement);

this._shortcuts.set(name, shortcut);

return shortcut;
}

registerShortcuts(shortcuts: KeyboardShortcuts): KeyboardShortcutListeners {
return shortcuts.map(shortcut => this.registerShortcut(shortcut));
}

getShortcut(name: string): KeyboardShortcutListener {
const shortcut = this._shortcuts.get(name);
const errorMessage = `No shortcut exists for ${name}, check the spelling or register one`;

return shortcut ?? throwError(() => new Error(errorMessage));
}

unregisterShortcut(name: string): boolean {
return this._shortcuts.delete(name);
}

unregisterAllShortcuts(): void {
this._shortcuts.clear();
}

private createShortcut(name: string, key: string, element: HTMLElement): KeyboardShortcutListener {
return new Observable(subscriber => {
const handler = (event: KeyboardEvent) => {
event.preventDefault()
subscriber.next({event, name});
};

const listener = this.eventManager.addEventListener(element, key, handler);

return () => listener();
})
}

private concatKeys(keys: ShortcutKeys): string {
return 'keydown.' + keys.join('.');
}
}