-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathkeyboard.js
162 lines (130 loc) · 3.83 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
import InputSource from '../inputSource';
import {RESPONSIVE_GAMEPAD_INPUTS} from '../constants';
// HTML Tags that can be focused on, where the library should be disabled
// https://www.w3schools.com/tags/ref_byfunc.asp
const INPUT_HTML_TAGS = [
'input',
'textarea',
'button',
'select',
'option',
'optgroup',
'label',
'datalist'
];
// Modified Keys Ignored
// https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/getModifierState
const MODIFIER_KEYS = [
"Alt",
"Control",
"Meta",
"OS"
];
export default class KeyboardInputSource extends InputSource {
constructor() {
super();
this.clearInputMap();
// Some settings pertaining to the keyboard
this.enableIgnoreWhenInputElementFocused();
this.enableIgnoreWhenModifierState();
// Our bound updateFunction
this._boundUpdateKeymapValues = this._updateKeymapValues.bind(this);
}
clearInputMap() {
// Create our keymap to our inputs
this.keymap = {};
Object.keys(RESPONSIVE_GAMEPAD_INPUTS).forEach(input => {
this.keymap[input] = {
keys: [],
value: undefined
};
});
}
enable() {
if (typeof window === "undefined") {
throw new Error('Keyboard can only be used with a browser environment');
return;
}
window.addEventListener('keyup', this._boundUpdateKeymapValues)
window.addEventListener('keydown', this._boundUpdateKeymapValues)
}
disable() {
if (typeof window === "undefined") {
throw new Error('Keyboard can only be used with a browser environment');
return;
}
window.removeEventListener('keyup', this._boundUpdateKeymapValues)
window.removeEventListener('keydown', this._boundUpdateKeymapValues)
}
getState() {
const state = {
...RESPONSIVE_GAMEPAD_INPUTS
};
Object.keys(this.keymap).forEach(key => {
state[key] = this.keymap[key].value;
});
// Remove any remainig strings I may have
Object.keys(state).forEach(stateKey => {
if (typeof state[stateKey] === 'string') {
delete state[stateKey];
}
});
return state;
}
enableIgnoreWhenInputElementFocused() {
this.ignoreWhenInputElementFocused = true;
}
disableIgnoreWhenInputElementFocused() {
this.ignoreWhenInputElementFocused = false;
}
enableIgnoreWhenModifierState() {
this.ignoreOnModifierState = true;
}
disableIgnoreWhenModifierState() {
this.ignoreOnModifierState = false;
}
setKeysToResponsiveGamepadInput(codes, responsiveGamepadInput) {
if (!codes || !responsiveGamepadInput || codes.length === 0) {
throw new Error('Could not set the specificed keyboard keys to input');
}
if (typeof codes === 'string') {
codes = [codes];
}
this.keymap[responsiveGamepadInput].keys = codes;
}
_isFocusedOnInputElement() {
return INPUT_HTML_TAGS.some((htmlTag) => {
if (document.activeElement && document.activeElement.tagName.toLowerCase() === htmlTag.toLowerCase()) {
return true;
}
return false;
});
}
_isInModifierState(event) {
return MODIFIER_KEYS.some(key => event.getModifierState(key) || event.code === key);
}
_updateKeymapValues(event) {
// Check if we should be ignoring the event
if (this.ignoreWhenInputElementFocused && this._isFocusedOnInputElement()) {
return;
}
if (this.ignoreOnModifierState && this._isInModifierState(event)) {
return;
}
event.preventDefault();
// Update the keymap accordingly to the key event
Object.keys(this.keymap).some(key => {
return this.keymap[key].keys.some(code => {
if (code === event.code) {
if (event.type === 'keydown') {
this.keymap[key].value = true;
} else {
this.keymap[key].value = false;
}
return true;
}
return false;
});
});
}
}