simple-keyboard/src/lib/services/PhysicalKeyboard.ts

195 lines
4.2 KiB
TypeScript
Raw Normal View History

import { KeyboardOptions, PhysicalKeyboardParams } from "../interfaces";
2019-07-14 01:08:51 -04:00
import Utilities from "../services/Utilities";
2018-10-24 18:18:24 -04:00
/**
* Physical Keyboard Service
*/
2018-09-23 23:37:46 -04:00
class PhysicalKeyboard {
2021-03-07 02:52:17 -05:00
getOptions: () => KeyboardOptions;
dispatch: any;
2018-10-24 18:18:24 -04:00
/**
* Creates an instance of the PhysicalKeyboard service
*/
constructor({ dispatch, getOptions }: PhysicalKeyboardParams) {
2018-10-24 18:18:24 -04:00
/**
* @type {object} A simple-keyboard instance
*/
2019-07-14 01:08:51 -04:00
this.dispatch = dispatch;
this.getOptions = getOptions;
2018-09-23 23:37:46 -04:00
2018-10-30 23:28:23 -04:00
/**
* Bindings
*/
2019-07-14 01:08:51 -04:00
Utilities.bindMethods(PhysicalKeyboard, this);
2018-09-23 23:37:46 -04:00
}
2021-03-07 02:52:17 -05:00
handleHighlightKeyDown(event: KeyboardEvent) {
2020-02-06 00:26:22 -05:00
const options = this.getOptions();
const buttonPressed = this.getSimpleKeyboardLayoutKey(event);
2018-09-23 23:37:46 -04:00
2021-03-07 02:52:17 -05:00
this.dispatch((instance: any) => {
2020-02-06 00:26:22 -05:00
const buttonDOM =
2019-07-14 01:08:51 -04:00
instance.getButtonElement(buttonPressed) ||
instance.getButtonElement(`{${buttonPressed}}`);
2019-07-14 01:08:51 -04:00
if (buttonDOM) {
buttonDOM.style.backgroundColor =
options.physicalKeyboardHighlightBgColor || "#dadce4";
2019-07-14 01:08:51 -04:00
buttonDOM.style.color =
options.physicalKeyboardHighlightTextColor || "black";
if (options.physicalKeyboardHighlightPress) {
buttonDOM.click();
}
2018-09-23 23:37:46 -04:00
}
});
2019-07-14 01:08:51 -04:00
}
2018-09-23 23:37:46 -04:00
2021-03-07 02:52:17 -05:00
handleHighlightKeyUp(event: KeyboardEvent) {
2020-02-06 00:26:22 -05:00
const buttonPressed = this.getSimpleKeyboardLayoutKey(event);
2021-03-07 02:52:17 -05:00
this.dispatch((instance: any) => {
2020-02-06 00:26:22 -05:00
const buttonDOM =
2019-07-14 01:08:51 -04:00
instance.getButtonElement(buttonPressed) ||
instance.getButtonElement(`{${buttonPressed}}`);
2018-09-23 23:37:46 -04:00
2019-07-14 01:08:51 -04:00
if (buttonDOM && buttonDOM.removeAttribute) {
buttonDOM.removeAttribute("style");
2018-09-23 23:37:46 -04:00
}
});
}
2018-10-24 18:18:24 -04:00
/**
* Transforms a KeyboardEvent's "key.code" string into a simple-keyboard layout format
* @param {object} event The KeyboardEvent
*/
2021-03-07 02:52:17 -05:00
getSimpleKeyboardLayoutKey(event: KeyboardEvent) {
let output = "";
const keyId = event.code || event.key || this.keyCodeToKey(event?.keyCode);
2018-09-23 23:37:46 -04:00
2019-02-15 21:49:27 -05:00
if (
keyId?.includes("Numpad") ||
keyId?.includes("Shift") ||
keyId?.includes("Space") ||
keyId?.includes("Backspace") ||
keyId?.includes("Control") ||
keyId?.includes("Alt") ||
keyId?.includes("Meta")
2019-02-15 21:49:27 -05:00
) {
output = event.code || "";
2018-09-23 23:37:46 -04:00
} else {
output = event.key || this.keyCodeToKey(event?.keyCode) || "";
2018-09-23 23:37:46 -04:00
}
return output.length > 1 ? output?.toLowerCase() : output;
}
2018-09-23 23:37:46 -04:00
/**
* Retrieve key from keyCode
*/
keyCodeToKey(keyCode: number) {
return {
8: "Backspace",
9: "Tab",
13: "Enter",
16: "Shift",
17: "Ctrl",
18: "Alt",
19: "Pause",
20: "CapsLock",
27: "Esc",
32: "Space",
33: "PageUp",
34: "PageDown",
35: "End",
36: "Home",
37: "ArrowLeft",
38: "ArrowUp",
39: "ArrowRight",
40: "ArrowDown",
45: "Insert",
46: "Delete",
48: "0",
49: "1",
50: "2",
51: "3",
52: "4",
53: "5",
54: "6",
55: "7",
56: "8",
57: "9",
65: "A",
66: "B",
67: "C",
68: "D",
69: "E",
70: "F",
71: "G",
72: "H",
73: "I",
74: "J",
75: "K",
76: "L",
77: "M",
78: "N",
79: "O",
80: "P",
81: "Q",
82: "R",
83: "S",
84: "T",
85: "U",
86: "V",
87: "W",
88: "X",
89: "Y",
90: "Z",
91: "Meta",
96: "Numpad0",
97: "Numpad1",
98: "Numpad2",
99: "Numpad3",
100: "Numpad4",
101: "Numpad5",
102: "Numpad6",
103: "Numpad7",
104: "Numpad8",
105: "Numpad9",
106: "NumpadMultiply",
107: "NumpadAdd",
109: "NumpadSubtract",
110: "NumpadDecimal",
111: "NumpadDivide",
112: "F1",
113: "F2",
114: "F3",
115: "F4",
116: "F5",
117: "F6",
118: "F7",
119: "F8",
120: "F9",
121: "F10",
122: "F11",
123: "F12",
144: "NumLock",
145: "ScrollLock",
186: ";",
187: "=",
188: ",",
189: "-",
190: ".",
191: "/",
192: "`",
219: "[",
220: "\\",
221: "]",
222: "'",
}[keyCode];
2018-09-23 23:37:46 -04:00
}
}
2019-02-15 21:49:27 -05:00
export default PhysicalKeyboard;