-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyLib.js
49 lines (42 loc) · 1.34 KB
/
keyLib.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
//KeyLib v1
var Key = {
_keys:[], LEFT:37, RIGHT:39, UP:38, DOWN:40, BACKSPACE:8, CAPSLOCK:20, CONTROL:17, DELETEKEY:46,
END:35, ENTER:13, ESCAPE:27, HOME:36, INSERT:45, TAB:9, PGDN:34, PGUP:33, SPACE:32, SHIFT:16
};
Key.onKeyDown = function () {};
Key.onKeyUp = function () {};
Key.isDown = function (key) {
return this._keys[key];
};
Key.addListener = function (o) {
if (typeof o.onKeyDown == "function") document.addEventListener("keydown", o.onKeyDown, false);
if (typeof o.onKeyUp == "function") document.addEventListener("keyup", o.onKeyUp, false);
};
Key.removeListener = function (o) {
if (typeof o.onKeyDown == "function") document.removeEventListener("keydown", o.onKeyDown, false);
if (typeof o.onKeyUp == "function") document.removeEventListener("keyup", o.onKeyUp, false);
};
Key.init = function () {
if (!document.addEventListener && document.attachEvent) {
document.addEventListener = function (t, f) {
document.attachEvent("on"+ t, f);
};
document.removeEventListener = function (t, f) {
document.detachEvent("on"+ t, f);
};
}
document.onkeydown = function (e) {
e = e?e:event;
Key._keys[e.keyCode] = true;
Key.onKeyDown(e);
};
document.onkeyup = function (e) {
e = e?e:event;
Key._keys[e.keyCode] = false;
Key.onKeyUp(e);
};
for (var num = 0; num < 256; num++) {
this._keys[num] = false;
}
};
Key.init();