-
Notifications
You must be signed in to change notification settings - Fork 0
/
Input.js
73 lines (60 loc) · 1.67 KB
/
Input.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
(() => {
const keyMap = {
49: 0x1, // 1
50: 0x2, // 2
51: 0x3, // 3
52: 0xC, // 4
81: 0x4, // Q
87: 0x5, // W
69: 0x6, // E
82: 0xD, // R
65: 0x7, // A
83: 0x8, // S
68: 0x9, // D
70: 0xE, // F
90: 0xA, // Z
88: 0x0, // X
67: 0xB, // C
86: 0xF, // V
}
class Input extends chip8.lib.EventEmitter {
constructor() {
super()
this.pauseToggle = document.getElementById('pause')
this.pressList = []
this.initPause()
this.initListeneres()
}
initListeneres() {
window.addEventListener("keydown", (e) => this.handleKeyDown(e), false);
window.addEventListener("keyup", (e) => this.handleKeyUp(e), false);
}
initPause() {
this.pauseToggle.onclick = () => {
const content = this.pauseToggle.innerHTML
content === 'pause' ? this.handlePause() : this.handleResume()
}
}
handlePause() {
this.pauseToggle.innerHTML = 'resume'
this.emit('pause')
}
handleResume() {
this.pauseToggle.innerHTML = 'pause'
this.emit('resume')
}
handleKeyUp(e) {
if (!keyMap[e.keyCode]) {
return
}
this.emit('keyUp', { key: keyMap[e.keyCode] })
}
handleKeyDown(e) {
if (!keyMap[e.keyCode]) {
return
}
this.emit('keyDown', { key: keyMap[e.keyCode] })
}
}
window.chip8.Input = Input
})()