-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyboard.js
100 lines (89 loc) · 2.29 KB
/
keyboard.js
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
// **************
// ** keyboard **
// **************
const ID_S = {
Escape: "ac",
Backspace: "backspace",
Delete: "backspace",
operators: {
"/": "divide",
"*": "multiply",
"-": "minus",
"+": "plus",
},
Enter: "equals",
".": "dot",
numbers: {
0: "num0",
1: "num1",
2: "num2",
3: "num3",
4: "num4",
5: "num5",
6: "num6",
7: "num7",
8: "num8",
9: "num9",
},
};
function addStyleToButton(id) {
const numButton = document.getElementById(id);
numButton.classList.add("pressed");
}
function removeStyleFromButton(id) {
const numButton = document.getElementById(id);
numButton.classList.remove("pressed");
}
function activateButtonKeyDown(event) {
// * numbers *
if (event.key in ID_S.numbers) {
addStyleToButton(ID_S.numbers[event.key]);
populateScreen(event.key);
}
// * operators *
if (event.key in ID_S.operators) {
addStyleToButton(ID_S.operators[event.key]);
handleOperatorButton(event.key);
}
// * equals *
if (ID_S[event.key] === "equals") {
addStyleToButton(ID_S[event.key]);
equals();
}
// * dot *
if (ID_S[event.key] === "dot") {
addStyleToButton(ID_S[event.key]);
dot();
}
// * backspace *
if (ID_S[event.key] === "backspace") {
addStyleToButton(ID_S[event.key]);
backspace();
}
// * ac *
if (ID_S[event.key] === "ac") {
addStyleToButton(ID_S[event.key]);
ac();
}
}
function activateButtonKeyUp(event) {
// * numbers *
if (event.key in ID_S.numbers)
removeStyleFromButton(ID_S.numbers[event.key]);
// * operators *
if (event.key in ID_S.operators)
removeStyleFromButton(ID_S.operators[event.key]);
// * equals *
if (ID_S[event.key] === "equals") removeStyleFromButton(ID_S[event.key]);
// * dot *
if (ID_S[event.key] === "dot") removeStyleFromButton(ID_S[event.key]);
// * backspace *
if (ID_S[event.key] === "backspace") removeStyleFromButton(ID_S[event.key]);
// * ac *
if (ID_S[event.key] === "ac") removeStyleFromButton(ID_S[event.key]);
}
function activateButtonsKeyboard() {
window.addEventListener("keydown", activateButtonKeyDown);
window.addEventListener("keyup", activateButtonKeyUp);
}
activateButtonsKeyboard();