-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathkeys.js
More file actions
104 lines (81 loc) · 1.72 KB
/
keys.js
File metadata and controls
104 lines (81 loc) · 1.72 KB
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
97
98
99
100
101
102
103
104
// Copyright 2014 Robert Scott Dionne. All rights reserved.
/**
* @param {Document} document
* @constructor
*/
bouncingball.Keys = function(document) {
/**
* @type {Document}
*/
this.document_ = document;
/**
* @type {Object}
*/
this.keys_ = {};
/**
* @type {Object}
*/
this.oldKeys_ = {};
};
bouncingball.Key = {
SHIFT: 16,
SPACE: 32,
LEFT: 37,
UP: 38,
RIGHT: 39,
DOWN: 40,
W: 87,
A: 65,
J: 74,
K: 75,
S: 83,
D: 68,
F: 70,
Q: 81,
R: 82,
X: 88,
Y: 89,
Z: 90,
N: 78,
P: 80,
LT: 188,
GT: 190,
QUESTION: 191,
BACKTICK: 192
};
/**
*
*/
bouncingball.Keys.prototype.install = function() {
this.document_.onkeydown = bouncingball.bind(this.handleKeyDown_, this);
this.document_.onkeyup = bouncingball.bind(this.handleKeyUp_, this);
};
bouncingball.Keys.prototype.uninstall = function() {
this.document_.onkeydown = this.document_.onkeyup = null;
};
bouncingball.Keys.prototype.handleKeyDown_ = function(event) {
console.log(event);
this.keys_[event.keyCode] = true;
return true;
};
bouncingball.Keys.prototype.handleKeyUp_ = function(event) {
this.keys_[event.keyCode] = false;
return true;
};
bouncingball.Keys.prototype.isHeld = function(key) {
return this.isPressed(key) && this.oldKeys_[key];
};
bouncingball.Keys.prototype.isPressed = function(key) {
return this.keys_[key];
};
bouncingball.Keys.prototype.justPressed = function(key) {
return this.isPressed(key) && !this.oldKeys_[key];
};
bouncingball.Keys.prototype.justReleased = function(key) {
return !this.isPressed(key) && this.oldKeys_[key];
};
bouncingball.Keys.prototype.update = function() {
for (var key in this.keys_) {
this.oldKeys_[key] = this.keys_[key];
}
};