-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeys.js
More file actions
37 lines (30 loc) · 770 Bytes
/
keys.js
File metadata and controls
37 lines (30 loc) · 770 Bytes
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
class Keys{
constructor(){
this.keys_down = {}
}
static start_listenting(){
let keys = new Keys();
addEventListener('keydown', function(ev){
if (typeof ev.code === 'string'){
keys.keys_down[ev.code] = true;
}
})
addEventListener('keyup', function(ev){
if (typeof ev.code === 'string'){
keys.keys_down[ev.code] = false;
}
})
return keys;
}
is_key_down(code){
return !!this.keys_down[code];
}
is_key_up(code){
return !this.keys_down[code];
}
keys_down_list(){
return Object.entries(this.keys_down)
.filter(kv => kv[1])
.map(kv => kv[0])
}
}