-
Notifications
You must be signed in to change notification settings - Fork 0
/
keys.js
78 lines (74 loc) · 2.17 KB
/
keys.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
import { Shot } from './starship.js';
import { isGame } from './game.js';
export const keys = {
ArrowLeft: false,
ArrowRight: false,
ArrowUp: false,
ArrowDown: false,
ArrowDown: false,
Space: false,
Enter: false,
p: false,
s: false,
m: false,
n: false,
d: false,
'0': false
};
export function onKeyDown(event) { // key down event
if (event.key === 'ArrowLeft') {
keys.ArrowLeft = true;
} else if (event.key === 'ArrowRight') {
keys.ArrowRight = true;
} else if (event.key === 'ArrowUp') {
keys.ArrowUp = true;
} else if (event.key === 'ArrowDown') {
keys.ArrowDown = true;
} else if (event.key === ' ') { // spacebar
keys.Space = true;
} else if (event.key === 'Enter' && isGame) {
keys.Enter = true;
} else if (event.key === 'p') {
keys.p = true;
} else if (event.key === 's') {
keys.s = true;
} else if (event.key === 'm') {
keys.m = true;
} else if (event.key === 'm') {
keys.n = true;
} else if (event.key === 'd') {
keys.d = true;
} else if (event.key === '0') {
keys['0'] = true;
}
}
export function onKeyUp(event) {// key up event
if (event.key === 'ArrowLeft') {
keys.ArrowLeft = false;
} else if (event.key === 'ArrowRight') {
keys.ArrowRight = false;
} else if (event.key === 'ArrowUp') {
keys.ArrowUp = false;
} else if (event.key === 'ArrowDown') {
keys.ArrowDown = false;
} else if (event.key === ' ') {
keys.Space = false;
} else if (event.key === 'Enter' && !isGame) {
keys.Enter = false;
} else if (event.key === 'p') {
keys.p = false;
} else if (event.key === 's') {
keys.s = false;
} else if (event.key === 'm') {
keys.m = false;
} else if (event.key === 'm') {
keys.n = false;
} else if (event.key === 'd') {
keys.d = false;
} else if (event.key === '0') {
keys['0'] = false;
}
}
document.addEventListener('keydown', onKeyDown);
document.addEventListener('keyup', onKeyUp);
document.addEventListener('keydown', Shot);