-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKeyboard.js
187 lines (180 loc) · 5.42 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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import {WebSystemObject} from './WebSystemObject.js';
/*
Clase para controlar el teclado...
Usage (to attach key press and released):
let teclado = new Keyboard(40, () => {
Game.controls.down = true
}, () => {
Game.controls.down = false
});
or just key pressed:
let teclado = new Keyboard(80, () => {
Game.togglePause();
});
or multiples:
let teclado = new Keyboard([[37, () => {Game.controls.left = true}, () => {Game.controls.left = false}],
[38, () => {Game.controls.up = true}, () => {Game.controls.up = false}],
[39, () => {Game.controls.right = true}, () => {Game.controls.right = false}],
[40, () => {Game.controls.down = true}, () => {Game.controls.down = false}],
[80, () => {Game.togglePause()}]]);
*/
export class Keyboard extends WebSystemObject {
static keyCode;
static keyDown;
static UpDownstates = [];
static DownEvents = [];
static UpEvents = [];
static keyBACKSPACE = 8;
static keyTAB = 9;
static keyENTER = 13;
static keySHIFT = 16;
static keyCTRL = 17;
static keyALT = 18;
static keyPAUSE_BREAK = 19;
static keyCAPS_LOCK = 20;
static keyESCAPE = 27;
static keySPACE = 32;
static keyPAGE_UP = 33;
static keyPAGE_DOWN = 34;
static keyEND = 35;
static keyHOME = 36;
static keyLEFT_ARROW = 37;
static keyUP_ARROW = 38;
static keyRIGHT_ARROW = 39;
static keyDOWN_ARROW = 40;
static keyINSERT = 45;
static keyDELETE = 46;
static key0 = 48;
static key1 = 49;
static key2 = 50;
static key3 = 51;
static key4 = 52;
static key5 = 53;
static key6 = 54;
static key7 = 55;
static key8 = 56;
static key9 = 57;
static keya = 65;
static keyb = 66;
static keyc = 67;
static keyd = 68;
static keye = 69;
static keyf = 70;
static keyg = 71;
static keyh = 72;
static keyi = 73;
static keyj = 74;
static keyk = 75;
static keyl = 76;
static keym = 77;
static keyn = 78;
static keyo = 79;
static keyp = 80;
static keyq = 81;
static keyr = 82;
static keys = 83;
static keyt = 84;
static keyu = 85;
static keyv = 86;
static keyw = 87;
static keyx = 88;
static keyy = 89;
static keyz = 90;
static keyLEFT_WINDOW = 91;
static keyRIGHT_WINDOW = 92;
static keySELECT = 93;
static keyNUMPAD_0 = 96;
static keyNUMPAD_1 = 97;
static keyNUMPAD_2 = 98;
static keyNUMPAD_3 = 99;
static keyNUMPAD_4 = 100;
static keyNUMPAD_5 = 101;
static keyNUMPAD_6 = 102;
static keyNUMPAD_7 = 103;
static keyNUMPAD_8 = 104;
static keyNUMPAD_9 = 105;
static keyMULTIPLY = 106;
static keyADD = 107;
static keySUBTRACT = 109;
static keyDECIMAL_POINT = 110;
static keyDIVIDE = 111;
static keyF1 = 112;
static keyF2 = 113;
static keyF3 = 114;
static keyF4 = 115;
static keyF5 = 116;
static keyF6 = 117;
static keyF7 = 118;
static keyF8 = 119;
static keyF9 = 120;
static keyF10 = 121;
static keyF11 = 122;
static keyF12 = 123;
static keyNUM_LOCK = 144;
static keySCROLL_LOCK = 145;
static keySEMI_COLON = 186;
static keyEQUAL_SIGN = 187;
static keyCOMMA = 188;
static keyDASH = 189;
static keyPERIOD = 190;
static keyFORWARD_SLASH = 191;
static keyGRAVE_ACENT = 192;
static keyOPEN_BRACKET = 219;
static keyBACK_SLASH = 220;
static keyCLOSE_BRACKET = 221;
static keySINGLE_QUOTE = 222;
static upDownStates = [];
static #upEvents = [];
static #downEvents = [];
constructor(keyCode, onPressed, onReleased) {
super();
if (arguments.length === 1 && arguments[0] instanceof Array) {
// Array of events
arguments[0].forEach((attach) => new Keyboard(...attach));
} else {
// The normal
if (Keyboard.#downEvents.length === 0 && Keyboard.#upEvents.length === 0) {
// window.addEventListener('keypress', Keyboard.#keyPress, false);
window.addEventListener('keyup', Keyboard.#keyUpEvent, false);
window.addEventListener('keydown', Keyboard.#keyDownEvent, false);
}
this.keyCode = keyCode;
this.onPressed = Object(onPressed);
Keyboard.#downEvents[this.keyCode] = this.onPressed;
if (onReleased) {
this.onReleased = Object(onReleased);
Keyboard.#upEvents[this.keyCode] = this.onReleased;
}
}
}
get keyChar() {
return String.fromCharCode(Keyboard.key);
}
static #keyDownEvent = (e) => {
Keyboard.keyDown = e.keyCode ? e.keyCode : 0;
Keyboard.upDownStates[Keyboard.keyDown] = true;
if (Keyboard.#downEvents[Keyboard.keyCode]) {
Keyboard.#downEvents[Keyboard.keyCode](e);
}
};
static #keyUpEvent = (e) => {
Keyboard.keyDown = e.keyCode ? e.keyCode : 0;
Keyboard.upDownStates[Keyboard.keyDown] = false;
if (Keyboard.#upEvents[Keyboard.keyCode]) {
Keyboard.#upEvents[Keyboard.keyCode](e);
}
};
free() {
if (this.onPressed) {
Keyboard.#downEvents = Keyboard.#downEvents.filter((event, Index) => Index !== Keyboard.keyCode);
}
if (this.onReleased) {
Keyboard.#downEvents = Keyboard.#upEvents.filter((event, Index) => Index !== Keyboard.keyCode);
}
if (Keyboard.#downEvents.length === 0 && Keyboard.#upEvents.length === 0) {
// window.removeEventListener('keypress', Keyboard.#keyPress);
window.removeEventListener('keyup', Keyboard.#keyUpEvent);
window.removeEventListener('keydown', Keyboard.#keyDownEvent);
}
}
}