Skip to content
This repository was archived by the owner on Oct 22, 2021. It is now read-only.

Commit b2b766e

Browse files
committed
✨ Allow detaching the tactile keyboard
Write in inputs other than the terminal emulator
1 parent a656094 commit b2b766e

File tree

1 file changed

+43
-6
lines changed

1 file changed

+43
-6
lines changed

src/classes/keyboard.class.js

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ class Keyboard {
66
const layout = JSON.parse(require("fs").readFileSync(opts.layout, {encoding: "utf-8"}));
77
const container = document.getElementById(opts.container);
88

9+
this.linkedToTerm = true;
10+
this.detach = () => {
11+
this.linkedToTerm = false;
12+
};
13+
this.attach = () => {
14+
this.linkedToTerm = true;
15+
};
16+
917
// Set default keyboard properties
1018
container.dataset.isShiftOn = false;
1119
container.dataset.isCapsLckOn = false;
@@ -886,13 +894,42 @@ class Keyboard {
886894
break;
887895
}
888896
} else if (cmd === "\n") {
889-
window.term[window.currentTerm].writelr("");
890-
} else if (cmd === ctrlseq[19] && window.term[window.currentTerm].term.hasSelection()) {
897+
if (window.keyboard.linkedToTerm) {
898+
window.term[window.currentTerm].writelr("");
899+
} else {
900+
// Do nothing, return not accepted in inputs
901+
}
902+
} else if (cmd === ctrlseq[19] && window.keyboard.linkedToTerm && window.term[window.currentTerm].term.hasSelection()) {
891903
window.term[window.currentTerm].clipboard.copy();
892-
} else if (cmd === ctrlseq[20] && window.term[window.currentTerm].clipboard.didCopy) {
904+
} else if (cmd === ctrlseq[20] && window.keyboard.linkedToTerm && window.term[window.currentTerm].clipboard.didCopy) {
893905
window.term[window.currentTerm].clipboard.paste();
894906
} else {
895-
window.term[window.currentTerm].write(cmd);
907+
if (window.keyboard.linkedToTerm) {
908+
window.term[window.currentTerm].write(cmd);
909+
} else {
910+
if (typeof document.activeElement.value !== "undefined") {
911+
switch(cmd) {
912+
case "":
913+
document.activeElement.value = document.activeElement.value.slice(0, -1);
914+
break;
915+
case "OD":
916+
document.activeElement.selectionStart--;
917+
document.activeElement.selectionEnd = document.activeElement.selectionStart;
918+
break;
919+
case "OC":
920+
document.activeElement.selectionEnd++;
921+
document.activeElement.selectionStart = document.activeElement.selectionEnd;
922+
break;
923+
default:
924+
if (ctrlseq.indexOf(cmd.slice(0, 1)) !== -1) {
925+
// Prevent trying to write other control sequences
926+
} else {
927+
document.activeElement.value = document.activeElement.value+cmd;
928+
}
929+
}
930+
}
931+
document.activeElement.focus();
932+
}
896933
}
897934
};
898935

@@ -917,7 +954,7 @@ class Keyboard {
917954
});
918955

919956
// Keep focus on the terminal
920-
window.term[window.currentTerm].term.focus();
957+
if (window.keyboard.linkedToTerm) window.term[window.currentTerm].term.focus();
921958

922959
window.audioManager.beep2.play();
923960
e.preventDefault();
@@ -958,7 +995,7 @@ class Keyboard {
958995
pressKey(key);
959996

960997
// Keep focus on the terminal
961-
window.term[window.currentTerm].term.focus();
998+
if (window.keyboard.linkedToTerm) window.term[window.currentTerm].term.focus();
962999

9631000
window.audioManager.beep3.play();
9641001
e.preventDefault();

0 commit comments

Comments
 (0)