dees-domtools/ts/domtools.classes.keyboard.ts

214 lines
3.8 KiB
TypeScript
Raw Normal View History

2022-04-21 16:01:54 +00:00
import * as plugins from './domtools.plugins.js';
2022-04-20 21:56:09 +00:00
export enum Key {
Backspace = 8,
Tab = 9,
Enter = 13,
Shift = 16,
Ctrl = 17,
Alt = 18,
PauseBreak = 19,
CapsLock = 20,
Escape = 27,
Space = 32,
PageUp = 33,
PageDown = 34,
End = 35,
Home = 36,
LeftArrow = 37,
UpArrow = 38,
RightArrow = 39,
DownArrow = 40,
Insert = 45,
Delete = 46,
Zero = 48,
ClosedParen = Zero,
One = 49,
ExclamationMark = One,
Two = 50,
AtSign = Two,
Three = 51,
PoundSign = Three,
Hash = PoundSign,
Four = 52,
DollarSign = Four,
Five = 53,
PercentSign = Five,
Six = 54,
Caret = Six,
Hat = Caret,
Seven = 55,
Ampersand = Seven,
Eight = 56,
Star = Eight,
Asterik = Star,
Nine = 57,
OpenParen = Nine,
A = 65,
B = 66,
C = 67,
D = 68,
E = 69,
F = 70,
G = 71,
H = 72,
I = 73,
J = 74,
K = 75,
L = 76,
M = 77,
N = 78,
O = 79,
P = 80,
Q = 81,
R = 82,
S = 83,
T = 84,
U = 85,
V = 86,
W = 87,
X = 88,
Y = 89,
Z = 90,
LeftWindowKey = 91,
RightWindowKey = 92,
SelectKey = 93,
Numpad0 = 96,
Numpad1 = 97,
Numpad2 = 98,
Numpad3 = 99,
Numpad4 = 100,
Numpad5 = 101,
Numpad6 = 102,
Numpad7 = 103,
Numpad8 = 104,
Numpad9 = 105,
Multiply = 106,
Add = 107,
Subtract = 109,
DecimalPoint = 110,
Divide = 111,
F1 = 112,
F2 = 113,
F3 = 114,
F4 = 115,
F5 = 116,
F6 = 117,
F7 = 118,
F8 = 119,
F9 = 120,
F10 = 121,
F11 = 122,
F12 = 123,
NumLock = 144,
ScrollLock = 145,
SemiColon = 186,
Equals = 187,
Comma = 188,
Dash = 189,
Period = 190,
UnderScore = Dash,
PlusSign = Equals,
ForwardSlash = 191,
Tilde = 192,
GraveAccent = Tilde,
OpenBracket = 219,
ClosedBracket = 221,
2022-04-21 16:01:54 +00:00
Quote = 222,
2022-04-20 21:56:09 +00:00
}
export class Keyboard {
2022-04-21 16:01:54 +00:00
private mapCombosToHandlers = new Map<number[], plugins.smartrx.rxjs.Subject<KeyboardEvent>>();
2022-04-20 21:56:09 +00:00
private pressedKeys = new Set<Key>();
2022-04-21 16:01:54 +00:00
constructor(private domNode: Element | Document) {
this.startListening();
2022-04-20 21:56:09 +00:00
}
2022-04-21 17:16:06 +00:00
public keyEnum = Key;
2022-04-21 21:52:45 +00:00
public on(keys: Key[]) {
2022-04-21 16:01:54 +00:00
const subject = new plugins.smartrx.rxjs.Subject<KeyboardEvent>();
2022-04-21 21:52:45 +00:00
this.registerKeys(keys, subject);
2022-04-21 17:05:52 +00:00
return subject;
2022-04-20 21:56:09 +00:00
}
2022-04-22 08:39:34 +00:00
public triggerKeyPress(keysArg: Key[]) {
for (const key of keysArg) {
this.pressedKeys.add(key);
}
this.checkMatchingKeyboardSubjects();
for (const key of keysArg) {
this.pressedKeys.delete(key);
}
}
2022-04-21 16:01:54 +00:00
public startListening() {
this.domNode.addEventListener('keydown', this.handleKeyDown);
this.domNode.addEventListener('keyup', this.handleKeyUp);
2022-04-20 21:56:09 +00:00
}
2022-04-21 16:01:54 +00:00
public stopListening() {
this.domNode.removeEventListener('keydown', this.handleKeyDown);
this.domNode.removeEventListener('keyup', this.handleKeyUp);
2022-04-20 21:56:09 +00:00
}
2022-04-21 16:01:54 +00:00
public clear() {
this.stopListening();
this.mapCombosToHandlers.clear();
this.pressedKeys.clear();
2022-04-20 21:56:09 +00:00
}
private handleKeyDown = (event: KeyboardEvent) => {
2022-04-21 16:01:54 +00:00
this.pressedKeys.add(event.keyCode);
2022-04-22 08:39:34 +00:00
this.checkMatchingKeyboardSubjects(event);
};
2022-04-20 21:56:09 +00:00
2022-04-22 08:39:34 +00:00
private checkMatchingKeyboardSubjects(payloadArg?) {
2022-04-21 21:52:45 +00:00
this.mapCombosToHandlers.forEach((subjectArg, keysArg) => {
if (this.areAllKeysPressed(keysArg)) {
2022-04-22 08:39:34 +00:00
subjectArg.next(payloadArg);
2022-04-21 16:01:54 +00:00
}
});
2022-04-22 08:39:34 +00:00
}
2022-04-20 21:56:09 +00:00
private handleKeyUp = (event: KeyboardEvent) => {
2022-04-21 16:01:54 +00:00
this.pressedKeys.delete(event.keyCode);
};
2022-04-20 21:56:09 +00:00
2022-04-22 07:25:10 +00:00
private areAllKeysPressed(keysArg: Key[]) {
2022-04-21 16:01:54 +00:00
let result = true;
2022-04-20 21:56:09 +00:00
2022-04-22 07:25:10 +00:00
keysArg.forEach((key) => {
2022-04-21 16:01:54 +00:00
if (!this.pressedKeys.has(key)) {
result = false;
2022-04-20 21:56:09 +00:00
}
2022-04-21 16:01:54 +00:00
});
2022-04-20 21:56:09 +00:00
2022-04-21 16:01:54 +00:00
return result;
}
2022-04-20 21:56:09 +00:00
2022-04-21 21:52:45 +00:00
private registerKeys(
keysArg: Array<Key>,
2022-04-21 16:01:54 +00:00
subjectArg: plugins.smartrx.rxjs.Subject<KeyboardEvent>
) {
2022-04-21 21:52:45 +00:00
if (!this.mapCombosToHandlers.has(keysArg)) {
this.mapCombosToHandlers.set(keysArg, subjectArg);
2022-04-21 16:01:54 +00:00
} else {
2022-04-21 21:52:45 +00:00
const subject = this.mapCombosToHandlers.get(keysArg);
2022-04-21 16:01:54 +00:00
return subject;
}
2022-04-20 21:56:09 +00:00
}
2022-04-21 16:01:54 +00:00
}