This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
demo.js
143 lines (123 loc) · 3.76 KB
/
demo.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
(function (window) {
var gamepadConfig = {
axisThreshold: 0,
indices: {
'standard': {
cursorX: 2,
cursorY: 3,
scrollX: 0,
scrollY: 1,
back: 9,
forward: 8,
vendor: 10,
zoomIn: 5,
zoomOut: 1
},
'46d-c216-Logitech Dual Action': {
cursorX: 3,
cursorY: 4,
scrollX: 1,
scrollY: 2,
back: 8,
forward: 9,
vendor: null,
zoomIn: 7,
zoomOut: 6
},
'79-6-Generic USB Joystick': {
cursorX: null,
cursorY: null,
scrollX: 3,
scrollY: 2,
back: 6,
forward: 7,
vendor: null,
zoomIn: 9,
zoomOut: 8
},
keyEvents: {
vendor: {
detail: {
charCode: 0,
key: 'Escape',
keyCode: 27
}
}
}
}
};
var gamepads = new Gamepads(gamepadConfig);
gamepads.polling = false;
if (gamepads.gamepadsSupported) {
gamepads.updateStatus = function () {
gamepads.polling = true;
gamepads.update();
window.requestAnimationFrame(gamepads.updateStatus);
};
gamepads.cancelLoop = function () {
gamepads.polling = false;
if (gamepads.pollingInterval) {
window.clearInterval(gamepads.pollingInterval);
}
window.cancelAnimationFrame(gamepads.updateStatus);
};
window.addEventListener('gamepadconnected', function (e) {
console.log('Gamepad connected at index %d: %s. %d buttons, %d axes.',
e.gamepad.index, e.gamepad.id, e.gamepad.buttons.length, e.gamepad.axes.length);
gamepads.updateStatus();
});
window.addEventListener('gamepaddisconnected', function (e) {
console.log('Gamepad removed at index %d: %s.', e.gamepad.index, e.gamepad.id);
});
if (gamepads.nonstandardEventsEnabled) {
window.addEventListener('gamepadaxismove', function (e) {
console.log('Gamepad axis move at index %d: %s. Axis: %d. Value: %f.',
e.gamepad.index, e.gamepad.id, e.axis, e.value);
});
window.addEventListener('gamepadbuttondown', function (e) {
console.log('Gamepad button down at index %d: %s. Button: %d.',
e.gamepad.index, e.gamepad.id, e.button);
});
window.addEventListener('gamepadbuttonup', function (e) {
console.log('Gamepad button up at index %d: %s. Button: %d.',
e.gamepad.index, e.gamepad.id, e.button);
});
}
if (gamepads.keyEventsEnabled) {
window.addEventListener('keydown', function (e) {
if (!e.gamepad) {
return;
}
console.log('Keydown event from gamepad button down at index %d: %s. Button: %d. Key: %s. Key code: %d.',
e.gamepad.index, e.gamepad.id, e.button, e.key || '?', e.keyCode);
});
window.addEventListener('keypress', function (e) {
if (!e.gamepad) {
return;
}
console.log('Keypress event from gamepad button down at index %d: %s. Button: %d. Key: %s. Key code: %d.',
e.gamepad.index, e.gamepad.id, e.button, e.key || '?', e.keyCode);
});
window.addEventListener('keyup', function (e) {
if (!e.gamepad) {
return;
}
console.log('Keyup event from gamepad button down at index %d: %s. Button: %d. Key: %s. Key code: %d.',
e.gamepad.index, e.gamepad.id, e.button, e.key || '?', e.keyCode);
});
}
// At the time of this writing, Firefox is the only browser that correctly
// fires the `gamepadconnected` event. For the other browsers
// <https://crbug.com/344556>, we start polling every 100ms until the
// first gamepad is connected.
if (Gamepads.utils.browser !== 'firefox') {
gamepads.pollingInterval = window.setInterval(function () {
if (gamepads.poll().length) {
gamepads.updateStatus();
window.clearInterval(gamepads.pollingInterval);
}
}, 100);
}
}
window.gamepads = gamepads;
})(window);