-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsKeyBox.js
126 lines (100 loc) · 2.66 KB
/
jsKeyBox.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// JsKeyBox !! A.K.A name = JKB
// A JavaJump Box for working with Key presses
function jsKeyBox(){}
jsKeyBox.prototype.Jump = function(){
this.name = "JKB";
this.errors = [];
this.shortCuts = [];
this.tracker = [];
this.keyLookup= {SHIFT:16, S:83};
};
jsKeyBox.prototype.eventListener = function(){
var controller = this;
var catchPress = function(e){
e = e || window.event;
target = e.target || window.event.srcElement;
if(e && target.nodeName != "INPUT"){
var code = e.keyCode || e.which;
if(!controller.tracker[0] || controller.tracker[0] != code){
controller.tracker.push(code);
}
if(controller.tracker.length >= 2){
var action = controller.execute();
if(action === true){
e.cancelBubble = true;
e.returnValue = false;
//e.stopPropagation works in Firefox.
if (e.stopPropagation) {
e.stopPropagation();
e.preventDefault();
}
}
}
}
};
var clearPress = function(e){
e = e || window.event;
if(e){
var code = e.keycode || e.which;
var positionCheck = controller.tracker.indexOf(code);
if(positionCheck != -1){
controller.tracker = [];
}
}else{
return false;
}
};
if(document.addEventListener){
document.addEventListener('keydown',catchPress,false);
document.addEventListener('keyup',clearPress,false);
}else if(document.attachEvent){
document.attachEvent('onkeydown',catchPress);
document.attachEvent('onkeyup',clearPress);
}else{
var err = {code:"1",detail:"Failed to add event listener"};
this.errors.push(err);
}
};
jsKeyBox.prototype.assignShortcut = function(str,callback){
if(typeof(str) == "undefined"){
this.errors.push({code:"2",detail:"No shortcut submitted"});
return false;
}else{
this.shortCuts.push({shortcut:str,action:callback});
this.eventListener();
}
return true;
};
jsKeyBox.prototype.unAssignShortcut = function(str){
if(typeof(str) == "undefined"){
this.errors.push({code:"2",detail:"No shortcut submitted"});
return false;
}else{
for (var i =0; i< this.shortCuts.length; i++){
if (this.shortCuts[i].shortcut == str){
this.shortCuts.splice(i,1);
}
}
this.eventListener();
}
return true;
};
jsKeyBox.prototype.getCode = function(keyName){
if(this.keyLookup[keyName]){
return this.keyLookup[keyName];
}
};
jsKeyBox.prototype.execute = function(){
for (var i = 0; i<this.shortCuts.length;i++){
var keys = this.shortCuts[i].shortcut.split("+");
if (this.tracker[0] == this.getCode(keys[0]) && this.tracker[1] == this.getCode(keys[1])){
this.shortCuts[i].action();
this.tracker = [];
return true;
}
}
this.tracker = [];
return false;
};
//jsKeyBox.prototype.
J.JumpStart(new jsKeyBox());