-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.ts
71 lines (60 loc) · 1.96 KB
/
input.ts
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
let Input = {
downKeys: {},
pressedKeys: {},
releasedKeys: {},
// Mouse status
mouse: {
x:0,
y:0,
down: false,
click: false,
onscreen:false
},
setMouseState(event) {
this.mouse.x = event.clientX;
this.mouse.y = event.clientY;
let leftclick = event.buttons & 1;
if (!this.mouse.down && leftclick) {
this.mouse.click = true;
}
this.mouse.down = (event.buttons & 1);
},
// register the event listeners
init(){
let me = this;
document.addEventListener('keydown', function(event) {
if (!me.downKeys[event.code]) {
me.pressedKeys[event.code] = true;
}
me.downKeys[event.code] = true;
console.log("Keydown: " + event.code);
});
document.addEventListener('keyup', function(event) {
me.downKeys[event.code] = false;
me.releasedKeys[event.code] = true;
});
document.onmousemove = this.setMouseState.bind(this);
document.onmousedown = this.setMouseState.bind(this);
document.onmouseup = this.setMouseState.bind(this);
document.onmouseleave = function() { me.mouseOnScreen = false; };
document.onmouseenter = function() { me.mouseOnScreen = true; };
},
// returns true if the key is currently held down
keyDown(key: string) {
return this.downKeys[key] == true;
},
// returns true if the key was pressed this update
keyPressed(key: string) {
return this.pressedKeys[key] == true;
},
// returns true if the key was released this update
keyReleased(key: string) {
return this.releasedKeys[key] == true;
},
// called to signal that a new update tick begins
update() {
this.pressedKeys = {};
this.releasedKeys = {};
this.mouse.click = false;
}
}