2021-11-03 04:21:58 +02:00
|
|
|
class KeyboardController {
|
|
|
|
constructor(connection) {
|
|
|
|
this.connection = connection;
|
2022-08-26 15:56:42 +03:00
|
|
|
this.container = null;
|
|
|
|
|
|
|
|
this.layouts = {
|
|
|
|
default: [
|
|
|
|
["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"],
|
|
|
|
["q", "w", "e", "r", "t", "y", "u", "i", "o", "p"],
|
|
|
|
["a", "s", "d", "f", "g", "h", "j", "k", "l"],
|
|
|
|
["🄰", "z", "x", "c", "v", "b", "n", "m", ".", "⌦"],
|
|
|
|
["🔢", " ", "↵"]
|
|
|
|
],
|
|
|
|
uppercase: [
|
|
|
|
["!", "@", "#", "$", "%", "^", "&", "*", "(", ")"],
|
|
|
|
["Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P"],
|
|
|
|
["A", "S", "D", "F", "G", "H", "J", "K", "L"],
|
|
|
|
["🅰", "Z", "X", "C", "V", "B", "N", "M", ",", "⌦"],
|
|
|
|
["🔢", " ", "↵"]
|
|
|
|
],
|
|
|
|
symbols: [
|
|
|
|
["!", "@", "#", "$", "%", "^", "&", "*", "(", ")"],
|
|
|
|
["[", "]", "{", "}", ";", ":", "'", "\"", ",", "<"],
|
|
|
|
[".", ">", "-", "_", "=", "+", "/", "?", "\\"],
|
|
|
|
["🄰", "|", "`", "~", "⌦"],
|
|
|
|
["🔤", " ", "↵"]
|
|
|
|
]
|
|
|
|
}
|
2021-11-03 04:21:58 +02:00
|
|
|
}
|
|
|
|
|
2021-11-04 02:07:37 +02:00
|
|
|
_sendKeyPress(l) {
|
2021-11-03 04:21:58 +02:00
|
|
|
this.connection.sendMessage("k", [l]);
|
|
|
|
}
|
|
|
|
|
2022-08-26 15:56:42 +03:00
|
|
|
makeKeyboardForLayout(layout) {
|
|
|
|
queueMicrotask(() => {
|
|
|
|
while (this.container.firstChild) {
|
|
|
|
this.container.removeChild(this.container.lastChild);
|
|
|
|
}
|
|
|
|
|
|
|
|
const rowsElement = document.createElement("div");
|
|
|
|
rowsElement.classList.add("Keyboard-rows");
|
|
|
|
|
|
|
|
for (let row = 0; row < layout.length; row++) {
|
|
|
|
const rowElement = document.createElement("div");
|
|
|
|
rowElement.classList.add("Keyboard-row");
|
|
|
|
for (let col = 0; col < layout[row].length; col++) {
|
|
|
|
const key = layout[row][col];
|
2021-11-08 00:25:27 +02:00
|
|
|
|
2022-08-26 15:56:42 +03:00
|
|
|
const colElement = document.createElement("button");
|
|
|
|
colElement.classList.add("Keyboard-button");
|
|
|
|
colElement.innerHTML = key;
|
|
|
|
colElement.addEventListener("click", (event) => {
|
|
|
|
this.handleKeypress(key);
|
|
|
|
});
|
2021-11-08 00:25:27 +02:00
|
|
|
|
2022-08-26 15:56:42 +03:00
|
|
|
rowElement.appendChild(colElement);
|
|
|
|
}
|
|
|
|
rowsElement.appendChild(rowElement);
|
2021-11-03 04:21:58 +02:00
|
|
|
}
|
2022-08-26 15:56:42 +03:00
|
|
|
|
|
|
|
this.container.appendChild(rowsElement);
|
2021-11-03 04:21:58 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-08-26 15:56:42 +03:00
|
|
|
handleKeypress(key) {
|
|
|
|
switch (key) {
|
|
|
|
case "🄰": {
|
|
|
|
this.makeKeyboardForLayout(this.layouts.uppercase);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "🅰": {
|
|
|
|
this.makeKeyboardForLayout(this.layouts.default);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "🔢": {
|
|
|
|
this.makeKeyboardForLayout(this.layouts.symbols);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "🔤": {
|
|
|
|
this.makeKeyboardForLayout(this.layouts.default);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case " ": {
|
|
|
|
this._sendKeyPress("{space}");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "⌦": {
|
|
|
|
this._sendKeyPress("{backspace}");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "↵": {
|
|
|
|
this._sendKeyPress("{ent}");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
this._sendKeyPress(key);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-11-08 00:25:27 +02:00
|
|
|
}
|
|
|
|
|
2022-08-26 15:56:42 +03:00
|
|
|
mountOn(element) {
|
|
|
|
if (this.container)
|
|
|
|
return; // Already mounted;
|
2021-11-03 04:21:58 +02:00
|
|
|
|
2022-08-26 15:56:42 +03:00
|
|
|
this.container = document.createElement("div");
|
|
|
|
this.container.classList.add("Keyboard");
|
|
|
|
element.appendChild(this.container);
|
|
|
|
|
|
|
|
this.makeKeyboardForLayout(this.layouts.default);
|
2021-11-03 04:21:58 +02:00
|
|
|
}
|
|
|
|
|
2022-08-26 15:56:42 +03:00
|
|
|
unmount() {
|
|
|
|
if (!this.container)
|
|
|
|
return; // Not mounted - don't do anything
|
|
|
|
|
|
|
|
this.container.parentElement.removeChild(this.container);
|
|
|
|
this.container = null;
|
2021-11-03 04:21:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default KeyboardController;
|