-
Notifications
You must be signed in to change notification settings - Fork 11
/
input.fakedevice.js
101 lines (89 loc) · 2.31 KB
/
input.fakedevice.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
(function() {
var keyCodeBlob = {
'Firefox' : {
'Mac' : [
72,
74,
75,
76,
87,
83,
65,
68
]
}
};
var osList = ['Win', 'Mac', 'Linux'];
function detectOS() {
for (var i in osList) {
if (navigator.platform.indexOf(osList[i]) !== -1) {
return osList[i];
}
}
return 'Unknown';
}
var browserList = ['Firefox', 'Chrome', 'Safari', 'Internet Explorer', 'Opera'];
function detectBrowser() {
for (var i in browserList) {
if (navigator.userAgent.indexOf(browserList[i]) !== -1) {
return browserList[i];
}
}
return 'Unknown';
}
function fakeButton(buttons, index, keyPressed) {
Object.defineProperty(buttons, index, {
enumerable: true,
get: function() { return keyPressed[index]; }
});
}
var FakeDevice = Input.FakeDevice = function() {
var browser = detectBrowser(),
os = detectOS(),
keyboard = keyCodeBlob,
keyPressed = [0, 0, 0, 0, 0, 0, 0, 0];
axes = this.axes = {},
buttons = this.buttons = {};
if (keyboard && keyboard[browser] && keyboard[browser][os]) {
keyboard = keyboard[browser][os];
} else {
throw "Oops, I didn't add fake gamepad support for " + browser + " on " + os;
}
window.addEventListener("keydown", function(e) {
for (var i in keyboard) {
if (e.keyCode === keyboard[i]) {
keyPressed[i] = 1;
return;
}
}
}, false);
window.addEventListener("keyup", function(e) {
for (var i in keyboard) {
if (e.keyCode === keyboard[i]) {
keyPressed[i] = 0;
return;
}
}
}, false);
window.addEventListener("blur", function(e) {
for (var i in keyPressed) {
keyPressed[i] = 0;
}
}, false);
for (var b in keyboard) {
fakeButton(buttons, b, keyPressed);
}
Object.defineProperty(this, "connected", {
enumerable: true,
get: function() { return true; }
});
Object.defineProperty(this, "id", {
enumerable: true,
get: function() { return "Firefox-Fake Gamepad-Bored on a plane industries"; }
});
Object.defineProperty(this, "index", {
enumerable: true,
get: function() { return -1; }
});
}
}());