-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientInput.js
More file actions
34 lines (28 loc) · 1.11 KB
/
ClientInput.js
File metadata and controls
34 lines (28 loc) · 1.11 KB
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
import EventSourceMixin from '../common/EventSourceMixin';
class ClientInput {
constructor(canvas) {
Object.assign(this, {
canvas,
keysPressed: new Set(), // клавиши, зажатые в данный момент
keyStateHandlers: {}, // обработчики, срабатывающие каждый рендер, если нажата клавиша
keyHandlers: {}, // обработчики при нажатии определенной клавиши
});
canvas.addEventListener('keydown', (e) => this.onKeyDown(e), false);
canvas.addEventListener('keyup', (e) => this.onKeyUp(e), false);
}
onKeyDown(e) {
this.keysPressed.add(e.code);
this.keyHandlers[e.code] && this.keyHandlers[e.code](true);
this.trigger('keydown', e);
}
onKeyUp(e) {
this.keysPressed.delete(e.code);
this.keyHandlers[e.code] && this.keyHandlers[e.code](false);
this.trigger('keyup', e);
}
onKey({ ...handlers }) {
this.keyHandlers = { ...this.keyHandlers, ...handlers };
}
}
Object.assign(ClientInput.prototype, EventSourceMixin);
export default ClientInput;