-
Notifications
You must be signed in to change notification settings - Fork 1
/
keys.js
96 lines (73 loc) · 1.48 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright 2011 ... All Rights Reserved.
/**
* @param {Document} document
* @constructor
*/
ray.Keys = function(document) {
/**
* @type {Document}
*/
this.document_ = document;
/**
* @type {Object}
*/
this.keys_ = {};
/**
* @type {Object}
*/
this.oldKeys_ = {};
};
ray.Key = {
LEFT: 37,
UP: 38,
RIGHT: 39,
DOWN: 40,
W: 87,
A: 65,
S: 83,
D: 68,
I: 73,
Q: 81,
Y: 89,
U: 85,
Z: 90,
N: 78,
P: 80,
LT: 188,
GT: 190
};
/**
*
*/
ray.Keys.prototype.install = function() {
this.document_.onkeydown = ray.bind(this.handleKeyDown_, this);
this.document_.onkeyup = ray.bind(this.handleKeyUp_, this);
};
ray.Keys.prototype.uninstall = function() {
this.document_.onkeydown = this.document_.onkeyup = null;
};
ray.Keys.prototype.handleKeyDown_ = function(event) {
this.keys_[event.keyCode] = true;
return true;
};
ray.Keys.prototype.handleKeyUp_ = function(event) {
this.keys_[event.keyCode] = false;
return true;
};
ray.Keys.prototype.isHeld = function(key) {
return this.isPressed(key) && this.oldKeys_[key];
};
ray.Keys.prototype.isPressed = function(key) {
return this.keys_[key];
};
ray.Keys.prototype.justPressed = function(key) {
return this.isPressed(key) && !this.oldKeys_[key];
};
ray.Keys.prototype.justReleased = function(key) {
return !this.isPressed(key) && this.oldKeys_[key];
};
ray.Keys.prototype.update = function() {
for (var key in this.keys_) {
this.oldKeys_[key] = this.keys_[key];
}
};