-
Notifications
You must be signed in to change notification settings - Fork 0
/
KeyboardState.js
112 lines (97 loc) · 3.18 KB
/
KeyboardState.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
* @author Lee Stemkoski
*
* Usage:
* (1) create a global variable:
* var keyboard = new KeyboardState();
* (2) during main loop:
* keyboard.update();
* (3) check state of keys:
* keyboard.down("A") -- true for one update cycle after key is pressed
* keyboard.pressed("A") -- true as long as key is being pressed
* keyboard.up("A") -- true for one update cycle after key is released
*
* See KeyboardState.k object data below for names of keys whose state can be polled
*/
// initialization
KeyboardState = function()
{
// bind keyEvents
document.addEventListener("keydown", KeyboardState.onKeyDown, false);
document.addEventListener("keyup", KeyboardState.onKeyUp, false);
}
///////////////////////////////////////////////////////////////////////////////
KeyboardState.k =
{
8: "backspace", 9: "tab", 13: "enter", 16: "shift",
17: "ctrl", 18: "alt", 27: "esc", 32: "space",
33: "pageup", 34: "pagedown", 35: "end", 36: "home",
37: "left", 38: "up", 39: "right", 40: "down",
45: "insert", 46: "delete", 186: ";", 187: "=",
188: ",", 189: "-", 190: ".", 191: "/",
219: "[", 220: "\\", 221: "]", 222: "'"
}
KeyboardState.status = {};
KeyboardState.keyName = function ( keyCode )
{
return ( KeyboardState.k[keyCode] != null ) ?
KeyboardState.k[keyCode] :
String.fromCharCode(keyCode);
}
KeyboardState.onKeyUp = function(event)
{
var key = KeyboardState.keyName(event.keyCode);
if ( KeyboardState.status[key] )
KeyboardState.status[key].pressed = false;
}
KeyboardState.onKeyDown = function(event)
{
var key = KeyboardState.keyName(event.keyCode);
if ( !KeyboardState.status[key] )
KeyboardState.status[key] = { down: false, pressed: false, up: false, updatedPreviously: false };
}
KeyboardState.prototype.update = function()
{
for (var key in KeyboardState.status)
{
// insure that every keypress has "down" status exactly once
if ( !KeyboardState.status[key].updatedPreviously )
{
KeyboardState.status[key].down = true;
KeyboardState.status[key].pressed = true;
KeyboardState.status[key].updatedPreviously = true;
}
else // updated previously
{
KeyboardState.status[key].down = false;
}
// key has been flagged as "up" since last update
if ( KeyboardState.status[key].up )
{
delete KeyboardState.status[key];
continue; // move on to next key
}
if ( !KeyboardState.status[key].pressed ) // key released
KeyboardState.status[key].up = true;
}
}
KeyboardState.prototype.down = function(keyName)
{
return (KeyboardState.status[keyName] && KeyboardState.status[keyName].down);
}
KeyboardState.prototype.pressed = function(keyName)
{
return (KeyboardState.status[keyName] && KeyboardState.status[keyName].pressed);
}
KeyboardState.prototype.up = function(keyName)
{
return (KeyboardState.status[keyName] && KeyboardState.status[keyName].up);
}
KeyboardState.prototype.debug = function()
{
var list = "Keys active: ";
for (var arg in KeyboardState.status)
list += " " + arg
console.log(list);
}
///////////////////////////////////////////////////////////////////////////////