-
Notifications
You must be signed in to change notification settings - Fork 25
/
gamepad.js
504 lines (494 loc) · 16.5 KB
/
gamepad.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
/*Created by Fry on 6/19/17.*/
var Gamepad = class Gamepad {
//////keyboard specific code
static remember_keydown(event){
Gamepad.the_down_keys[event.keyCode] = true
//event.stopPropagation() //doens't work in stopping keystokes going to the editor.
//so just clock mouse on another pane first.
}
static remember_keyup(event){
Gamepad.the_down_keys[event.keyCode] = false
//event.stopPropagation()
}
//this is effectively, "init". Do it once per dde session,
//or whenever a key is "stuck down" and you want to clear
//all "down" keys. Don't do it in the middle of a loop
//as it will undo any legit down keys
static start_remembering_keydown(){
Gamepad.the_down_keys = new Array(256).fill(false) //always init
if(!Gamepad.is_remembering_keydown){
window.addEventListener("keydown", Gamepad.remember_keydown)
window.addEventListener("keyup", Gamepad.remember_keyup)
Gamepad.is_remembering_keydown = true
}
}
static stop_remembering_keydown(){
window.removeEventListener("keydown", Gamepad.remember_keydown)
window.removeEventListener("keyup", Gamepad.remember_keydown)
Gamepad.is_remembering_keydown = false
}
static keycode_to_keyname(keycode){ //keycode is NOT the ascii integer. keyname is the char, ie "a" or "3"
return Gamepad.keycode_to_keyname_map[keycode]
}
///////gamepad specific code
//good for axes too
static gamecode_to_gamename(but_or_axes_code){
if (but_or_axes_code >= 0) { // 0 to 16 or so
if (but_or_axes_code < Gamepad.gamecode_to_gamename_map.length){
return Gamepad.gamecode_to_gamename_map[but_or_axes_code]
}
else { return "button" + but_or_axes_code }
}
else { //but_code is really an axis_code from -1 on down.
let i = (but_or_axes_code + 1) * -1 //i is now axes names array index
if (i < Gamepad.axes_names.length){
return Gamepad.axes_names[i]
}
else { return "axes" + but_or_axes_code }
}
}
static gamename_to_gamecode(gamename){
for (var gamecode = 0; gamecode < Gamepad.gamecode_to_keycode_map.length; gamecode++) {
var a_gamename = Gamepad.gamecode_to_gamename(gamecode)
if (a_gamename == gamename) { return gamecode }
}
for (var i = 0; i < Gamepad.axes_names.length; i++) {
var a_gamename = Gamepad.axes_names[i]
if (a_gamename == gamename) { return (i + 1) * -1 } //returns -1, -2, -3 or -4
}
if (gamename.startsWith("button")) {
var gamecode_str = gamename.substring(6)
if(is_string_a_integer(gamecode_str)){
return Number.parseInt(gamecode_str)
}
else { return null }
}
return null
}
/////keyboard & gamepad code
//return an array of literal objects, each one standing for a key that's down
//this is the main user function.
/*a,b,x,y l(eft bumper) r(ight bumper)
[ left_stick ] right_stick
v view, m menu
1-> 4 left trigger for values 0.25, 0.5. 0.71. 1
and 6->9 right trigger
arrows: up, down, left, right*/
//may return null
static gamecode_to_keycode(gamecode){ //gamecode is an integer 0 through 17 or so depending on the gamepad.
if (gamecode >= Gamepad.gamecode_to_keycode_map.length) { return null }
else if (gamecode < 0) { return null } //game axis, not a game button.
else { return Gamepad.gamecode_to_keycode_map[gamecode] }
}
//may return null
static keycode_to_gamecode(keycode){ //gamecode is an integer 0 through 17 or so depending on the gamepad.
for (var gamecode = 0; gamecode < Gamepad.gamecode_to_keycode_map.length; gamecode++) {
var a_keycode = Gamepad.gamecode_to_keycode_map[gamecode]
if (a_keycode == keycode) { return gamecode }
}
var keyname = Gamepad.keycode_to_keyname(keycode)
if (["1", "2", "3", "4"].includes(keyname)) {
return Gamepad.gamename_to_gamecode("LEFT_TRIGGER")
}
else if (["5", "6", "7", "8"].includes(keyname)) {
return Gamepad.gamename_to_gamecode("RIGHT_TRIGGER")
}
else { return null }
}
static down_keys(device="keyboard_gamepad", which_gamepad=0){
const result = []
if (device.includes("keyboard")){
if (!Gamepad.is_remembering_keydown){
Gamepad.start_remembering_keydown()
}
for(var keycode = 0; keycode < Gamepad.the_down_keys.length; keycode++){
if(Gamepad.the_down_keys[keycode]) {
var gamecode = Gamepad.keycode_to_gamecode(keycode)
var gamename = Gamepad.gamecode_to_gamename(gamecode)
var keyname = Gamepad.keycode_to_keyname(keycode)
var val = 1
switch(keyname){
case "1": val = 0.25; break;
case "2": val = 0.5; break;
case "3": val = 0.75; break;
case "4": val = 1; break;
case "5": val = 0.25; break;
case "6": val = 0.5. break;
case "7": val = 0.75; break;
case "8": val = 1; break;
}
result.push({device: "keyboard",
gamecode: gamecode,
gamename: gamename,
keycode: keycode,
keyname: keyname,
value: val
})
}
}
}
if (device.includes("gamepad")){
var gp = navigator.getGamepads()[which_gamepad]
if (!gp && !device.includes("keyboard")) { //if there's no gamepad AND we're not allowing kwyboard input, then no possibility of any input so error
if (which_gamepad > 3) {
dde_error("Usually, only 4 gamepads can be used (0 thru 3) but you tried for: " + which_gamepad)
}
else { dde_error("No gamepad at index: " + which_gamepad + " found." +
"<br/>Make sure a gamepad is plugged into a USB port," +
"<br/>Hit a button on it, and try again.")
}
}
else if (gp) {
const num_of_buttons = gp.buttons.length
for (let gamecode = 0; gamecode < num_of_buttons; gamecode++){
var but = gp.buttons[gamecode]
if(but.pressed){
let gamename = Gamepad.gamecode_to_gamename(gamecode)
let keycode = Gamepad.gamecode_to_keycode(gamecode)
let keyname = (keycode ? Gamepad.keycode_to_keyname(keycode) : null)
result.push({device: "gamepad",
gamecode: gamecode,
gamename: gamename,
keycode: keycode,
keyname: keyname,
value: 1
})
}
}
const num_of_axes = gp.axes.length
for(let i = 0; i < num_of_axes; i++){
let val = gp.axes[i]
if ((val >= 0.1) || (val <= -0.1)) { //joysticks are "noisy" so filter out values close to 0
let axes_num = (i + 1) * -1 //axes_num is typically -1 thru -4
var axes_name = gamepad_button_number_to_name(axes_num)
result.push({gamename: axes_name,
gamecode: axes_num, //will be -1, -2, -3, or -4
keycode: null,
keyname: null,
value: val //will not be 0
})
}
}
}
}
return result
}
}
Gamepad.is_remembering_keydown = false
// Gamepad.start_remembering_keydown()
// Gamepad.down_keys()
//from https://stackoverflow.com/questions/1772179/get-character-value-from-keycode-in-javascript-then-trim/5829387#5829387
//array index is a keyCode and the corresponding value
//is the "name" of the key. Note that there's
//just 1 set of letters (upper case) because
//there's just one set of letters on a keyboard.
//the "name" is generally the name printed on the key thouse these vary slightly.
//this has nothing to do with ascii codes.
//this maps keycode (0 to 255) to keyname (a string)
//speical keys:
// fn on both mac and windows does not get an event
//both shift keys, Alt/option keys and command/windows keys retun the same keycode
Gamepad.keycode_to_keyname_map = [
"", // [0]
"", // [1]
"", // [2]
"CANCEL", // [3]
"", // [4]
"", // [5]
"HELP", // [6]
"", // [7]
"BACK_SPACE", // [8] sometimes called "delete"
"TAB", // [9]
"", // [10]
"", // [11]
"CLEAR", // [12]
"ENTER", // [13]
"ENTER_SPECIAL", // [14]
"", // [15]
"SHIFT", // [16]
"CONTROL", // [17]
"ALT", // [18] called "option" on Mac
"PAUSE", // [19]
"CAPS_LOCK", // [20]
"KANA", // [21]
"EISU", // [22]
"JUNJA", // [23]
"FINAL", // [24]
"HANJA", // [25]
"", // [26]
"ESCAPE", // [27]
"CONVERT", // [28]
"NONCONVERT", // [29]
"ACCEPT", // [30]
"MODECHANGE", // [31]
" ", //"SPACE", // [32]
"PAGE_UP", // [33]
"PAGE_DOWN", // [34]
"END", // [35]
"HOME", // [36]
"LEFT", // [37]
"UP", // [38]
"RIGHT", // [39]
"DOWN", // [40]
"SELECT", // [41]
"PRINT", // [42]
"EXECUTE", // [43]
"PRINTSCREEN", // [44]
"INSERT", // [45]
"DELETE", // [46]
"", // [47]
"0", // [48]
"1", // [49]
"2", // [50]
"3", // [51]
"4", // [52]
"5", // [53]
"6", // [54]
"7", // [55]
"8", // [56]
"9", // [57]
"COLON", // [58]
"SEMICOLON", // [59]
"LESS_THAN", // [60]
"EQUALS", // [61]
"GREATER_THAN", // [62]
"QUESTION_MARK", // [63]
"AT", // [64]
"A", // [65]
"B", // [66]
"C", // [67]
"D", // [68]
"E", // [69]
"F", // [70]
"G", // [71]
"H", // [72]
"I", // [73]
"J", // [74]
"K", // [75]
"L", // [76]
"M", // [77]
"N", // [78]
"O", // [79]
"P", // [80]
"Q", // [81]
"R", // [82]
"S", // [83]
"T", // [84]
"U", // [85]
"V", // [86]
"W", // [87]
"X", // [88]
"Y", // [89]
"Z", // [90]
"OS_KEY", // [91] Windows Key (Windows) or Command Key (Mac)
"", // [92]
"CONTEXT_MENU", // [93]
"", // [94]
"SLEEP", // [95]
"NUMPAD0", // [96]
"NUMPAD1", // [97]
"NUMPAD2", // [98]
"NUMPAD3", // [99]
"NUMPAD4", // [100]
"NUMPAD5", // [101]
"NUMPAD6", // [102]
"NUMPAD7", // [103]
"NUMPAD8", // [104]
"NUMPAD9", // [105]
"MULTIPLY", // [106]
"ADD", // [107]
"SEPARATOR", // [108]
"SUBTRACT", // [109]
"DECIMAL", // [110]
"DIVIDE", // [111]
"F1", // [112]
"F2", // [113]
"F3", // [114]
"F4", // [115]
"F5", // [116]
"F6", // [117]
"F7", // [118]
"F8", // [119]
"F9", // [120]
"F10", // [121]
"F11", // [122]
"F12", // [123]
"F13", // [124]
"F14", // [125]
"F15", // [126]
"F16", // [127]
"F17", // [128]
"F18", // [129]
"F19", // [130]
"F20", // [131]
"F21", // [132]
"F22", // [133]
"F23", // [134]
"F24", // [135]
"", // [136]
"", // [137]
"", // [138]
"", // [139]
"", // [140]
"", // [141]
"", // [142]
"", // [143]
"NUM_LOCK", // [144]
"SCROLL_LOCK", // [145]
"WIN_OEM_FJ_JISHO", // [146]
"WIN_OEM_FJ_MASSHOU", // [147]
"WIN_OEM_FJ_TOUROKU", // [148]
"WIN_OEM_FJ_LOYA", // [149]
"WIN_OEM_FJ_ROYA", // [150]
"", // [151]
"", // [152]
"", // [153]
"", // [154]
"", // [155]
"", // [156]
"", // [157]
"", // [158]
"", // [159]
"CIRCUMFLEX", // [160]
"EXCLAMATION", // [161]
"DOUBLE_QUOTE", // [162]
"HASH", // [163]
"DOLLAR", // [164]
"PERCENT", // [165]
"AMPERSAND", // [166]
"UNDERSCORE", // [167]
"OPEN_PAREN", // [168]
"CLOSE_PAREN", // [169]
"ASTERISK", // [170]
"PLUS", // [171]
"PIPE", // [172]
"HYPHEN_MINUS", // [173]
"OPEN_CURLY_BRACKET", // [174]
"CLOSE_CURLY_BRACKET", // [175]
"TILDE", // [176]
"", // [177]
"", // [178]
"", // [179]
"", // [180]
"VOLUME_MUTE", // [181]
"VOLUME_DOWN", // [182]
"VOLUME_UP", // [183]
"", // [184]
"", // [185]
";", // "SEMICOLON", // [186]
"=", //"EQUALS", // [187]
",", //"COMMA", // [188]
"-", //"MINUS", // [189]
".", //"PERIOD", // [190]
"/", //"SLASH", // [191]
"`", //"BACK_QUOTE", // [192]
"", // [193]
"", // [194]
"", // [195]
"", // [196]
"", // [197]
"", // [198]
"", // [199]
"", // [200]
"", // [201]
"", // [202]
"", // [203]
"", // [204]
"", // [205]
"", // [206]
"", // [207]
"", // [208]
"", // [209]
"", // [210]
"", // [211]
"", // [212]
"", // [213]
"", // [214]
"", // [215]
"", // [216]
"", // [217]
"", // [218]
"[", //"OPEN_BRACKET", // [219]
"\\", //"BACK_SLASH", // [220]
"]", //"CLOSE_BRACKET", // [221]
"'", //"QUOTE", // [222]
"", // [223]
"META", // [224]
"ALTGR", // [225]
"", // [226]
"WIN_ICO_HELP", // [227]
"WIN_ICO_00", // [228]
"", // [229]
"WIN_ICO_CLEAR", // [230]
"", // [231]
"", // [232]
"WIN_OEM_RESET", // [233]
"WIN_OEM_JUMP", // [234]
"WIN_OEM_PA1", // [235]
"WIN_OEM_PA2", // [236]
"WIN_OEM_PA3", // [237]
"WIN_OEM_WSCTRL", // [238]
"WIN_OEM_CUSEL", // [239]
"WIN_OEM_ATTN", // [240]
"WIN_OEM_FINISH", // [241]
"WIN_OEM_COPY", // [242]
"WIN_OEM_AUTO", // [243]
"WIN_OEM_ENLW", // [244]
"WIN_OEM_BACKTAB", // [245]
"ATTN", // [246]
"CRSEL", // [247]
"EXSEL", // [248]
"EREOF", // [249]
"PLAY", // [250]
"ZOOM", // [251]
"", // [252]
"PA1", // [253]
"WIN_OEM_CLEAR", // [254]
"" // [255]
]
Gamepad.axes_names = [
//axis name //gamecode
"LEFT_STICK_X", //-1
"LEFT_STICK_Y", //-2
"RIGHT_STICK_X", //-3
"RIGHT_STICK_Y" //-4
]
Gamepad.gamecode_to_gamename_map = [ //xbox one button names from https://support.xbox.com/en-US/xbox-one/accessories/xbox-one-wireless-controller
"A", //0 right thumb 0 or 1
"B", //1 right thumb 0 or 1
"X", //2 right thumb 0 or 1
"Y", //3 right thumb 0 or 1
"LEFT_BUMPER", //4 left forefinger (above trigger) 0 or 1
"RIGHT_BUMPER", //5 right forefinger (above trigger) 0 or 1
"LEFT_TRIGGER", //6 left forefinger 0 to 1
"RIGHT_TRIGGER", //7 right forefinger 0 to 1
"VIEW", //8 left thumb (near center little button) 0 or 1
"MENU", //9 right thumb (near center little button) 0 or 1
"LEFT_STICK", //10 left thumb joystick button 0 or 1 and horizontal -1 to 1 and vertical -1 to 1
"RIGHT_STICK", //11 right thumb joystick button 0 or 1 and horizontal -1 to 1 and vertical -1 to 1
"UP", //12 left thumb Direction pad 0 or 1
"DOWN", //13 left thumb Direction pad 0 or 1
"LEFT", //14 left thumb Direction pad 0 or 1
"RIGHT", //15 left thumb Direction pad 0 or 1
"OS_KEY" //16 XBOX either thumb (top center of gamepad) 0 or 1
]
Gamepad.gamecode_to_keycode_map = [
//keycode, gamecode, keyname, gamename
65, //0, A A
66, //1, B B
88, //2, X X
89, //3, Y Y
76, //4, L LEFT BUMPER
82, //5, R RIGHT BUMPER
52, //6 4 LEFT_TRIGGER (really keynames 1, 2, 3, 4)
57, //7 9 RIGHT_TRIGGER (really keynames 5 ,6, 7, 8)
86, //8 V VIEW
77, //9 M MENU
219, //10 [ LEFT_STICK
221, //11 ] RIGHT_STICK
38, //12 UP UP
40, //13 DOWN DOWN
37, //14 LEFT LEFT
39, //15 RIGHT RIGHT
91 //16 OS_KEY OS_KEY
]