-
Notifications
You must be signed in to change notification settings - Fork 1
/
jquery.shortkeys.js
98 lines (93 loc) · 2.16 KB
/
jquery.shortkeys.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
jQuery.fn.shortkeys = jQuery.fn.keys = function (obj, settings) {
var el = this;
this.settings = jQuery.extend({
split: "+",
moreKeys: {}
}, settings || {});
this.wackyKeys = { '.': 190, ',': 188, ';': 59, 'Space': 32 };
this.formElements = "input,select,textarea,button";
this.keys = new Array();
this.onFormElement = false;
this.keysDown = new Array();
this.init = function (obj) {
for(x in this.wackyKeys) {
this.wackyKeys[x.toUpperCase()] = this.wackyKeys[x];
}
for(x in obj) {
this.keys.push(x.split(this.settings.split));
}
for(i in this.keys) {
var quickArr = new Array();
for(j in this.keys[i]) {
quickArr.push(this.convertToNumbers(this.keys[i][j].toUpperCase()));
}
quickArr.sort();
this.keys[i] = quickArr;
}
};
this.convertToNumbers = function (inp) {
if (this.wackyKeys[inp] != undefined) {
return this.wackyKeys[inp];
}
return inp.toUpperCase().charCodeAt(0);
};
this.keyAdd = function(keyCode) {
this.keysDown.push(keyCode);
this.keysDown.sort();
};
this.keyRemove = function (keyCode) {
for(i in this.keysDown) {
if(this.keysDown[i] == keyCode) {
this.keysDown.splice(i,1);
}
};
this.keysDown.sort();
};
this.keyTest = function (i) {
if (this.keys[i].length != this.keysDown.length) return false;
for(j in this.keys[i]) {
if(this.keys[i][j] != this.keysDown[j]) {
return false;
}
}
return true;
};
this.keyRemoveAll = function () {
this.keysDown = new Array();
};
this.focused = function (bool) {
this.onFormElement = bool;
}
$(document).keydown(function(e) {
el.keyAdd(e.keyCode);
var i = 0;
for(x in obj) {
if(el.keyTest(i) && !el.onFormElement) {
obj[x]();
return false;
break;
}
i++;
};
});
$(document).keyup(function (e) {
el.keyRemove(e.keyCode);
});
$.extend({
notFormElements: function() {
$(el.formElements).focus(function(){
el.focused(true);
});
$(el.formElements).blur(function(){
el.focused(false);
});
}
});
$.notFormElements();
$(document).focus( function () {
el.keyRemoveAll();
});
this.init(obj);
jQuery.extend(this.wackyKeys, this.settings.moreKeys);
return this;
}