-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeyboard.js
More file actions
125 lines (112 loc) · 2.21 KB
/
Copy pathkeyboard.js
File metadata and controls
125 lines (112 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
let counter = 0;
let text = "";
// mapping of which keys fire a 1 and which keys fire a 0
const ONE = [
"1",
"2",
"3",
"4",
"5",
"6",
"q",
"w",
"e",
"r",
"t",
"y",
"a",
"s",
"d",
"f",
"g",
"z",
"x",
"c",
"v",
"b",
];
const ZERO = [
"7",
"8",
"9",
"0",
"-",
"=",
"u",
"i",
"o",
"p",
"]",
"]",
"\\",
"h",
"j",
"k",
"l",
";",
"'",
"n",
"m",
",",
".",
"/",
];
let timeWindow = 50; // time in ms
let keysPressed = 0;
// get text
const textbox = document.querySelector("#textbox");
const input = document.querySelector("#input");
document.addEventListener("keydown", (event) => {
keysPressed++;
// get key
const key = event.key.toLowerCase();
let text = textbox.innerHTML;
let inputText = input.innerHTML;
if (inputText === "Type Here!" || inputText === "Copied to Clipboard") {
inputText = "";
}
// let backspace rapid fire
if (key === "backspace" || key === "delete") {
counter = Math.max(counter - 1, 0);
inputText = inputText.slice(0, -1);
}
// prevent multiple keys from firing at the same time
event.preventDefault();
if (event.repeat) return;
if (keysPressed != 1) return;
if (keysPressed == 1 && ZERO.includes(key)) {
inputText += "0";
counter++;
} else if (keysPressed == 1 && ONE.includes(key)) {
inputText += "1";
counter++;
}
if (counter === 8) {
counter = 0;
// get last byte
let int = parseInt(inputText, 2);
inputText = inputText.slice(0, -8);
// backspace character
if (int == 8) {
// remove previous character
text = text.slice(0, -1);
counter = Math.max(counter - 1, 0);
} else if (int == 42) {
navigator.clipboard.writeText(textbox.value);
// Alert the copied text
inputText = "Copied to Clipboard";
} else {
let byte = String.fromCharCode(int);
inputText = inputText.slice(0, -8);
console.log(byte);
text += byte;
}
}
console.log(counter);
textbox.innerHTML = text;
input.innerHTML = inputText;
document.documentElement.scrollTo({ top: document.documentElement.scrollHeight, behavior: "smooth" });
});
document.addEventListener("keyup", (event) => {
keysPressed--;
});