-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.keycontroller.js
90 lines (88 loc) · 2.76 KB
/
jquery.keycontroller.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
/*\
|*| IE-specific polyfill which enables the passage of arbitrary arguments to the
|*| callback functions of javascript timers (HTML5 standard syntax).
\*/
if (document.all && !window.setTimeout.isPolyfill) {
var __nativeST__ = window.setTimeout;
window.setTimeout = function (vCallback, nDelay /*, argumentToPass1, argumentToPass2, etc. */) {
var aArgs = Array.prototype.slice.call(arguments, 2);
return __nativeST__(vCallback instanceof Function ? function () {
vCallback.apply(null, aArgs);
} : vCallback, nDelay);
};
window.setTimeout.isPolyfill = true;
}
if (document.all && !window.setInterval.isPolyfill) {
var __nativeSI__ = window.setInterval;
window.setInterval = function (vCallback, nDelay /*, argumentToPass1, argumentToPass2, etc. */) {
var aArgs = Array.prototype.slice.call(arguments, 2);
return __nativeSI__(vCallback instanceof Function ? function () {
vCallback.apply(null, aArgs);
} : vCallback, nDelay);
};
window.setInterval.isPolyfill = true;
}
/**
* KeyController
*
* Offers a continuous key press event on multiple keys at a customised refresh rate. Allows the setting of a different callback function for when the key is pressed and to when it has been released.
* @author Aaron Harding <aaron.harding@graphitedigital.com>
*/
function KeyController() {
_this = this;
_this.defaults = {
'continuous' : false,
'rate' : 30
}
_this.continuousCallsHandler = {};
_this.activeKeys = {};
_this.controller = {};
_this.count = 0;
}
KeyController.prototype = {
init: function() {
$(document).keydown(function(event) {
for (var key in _this.controller) {
if(_this.controller.hasOwnProperty(key)) {
var obj = _this.controller[key];
if(obj.hasOwnProperty("key")) {
if(obj.key == (event.keyCode || event.which) && !_this.activeKeys[obj.key]) {
_this.activeKeys[obj.key] = true;
obj.down();
if(obj.continuous)
_this.continuousCallsHandler[obj.key] = setInterval(_this.downKeyCallback, 1000 / obj.rate, obj);
break;
}
}
}
}
});
$(document).keyup(function(event) {
for (var key in _this.controller) {
if(_this.controller.hasOwnProperty(key)) {
var obj = _this.controller[key];
if(obj.hasOwnProperty("key")) {
if(obj.key == (event.keyCode || event.which)) {
_this.activeKeys[obj.key] = false;
if(obj.up)
obj.up();
break;
}
}
}
}
});
},
downKeyCallback: function(obj) {
if (!_this.activeKeys[obj.key]) {
clearInterval(_this.continuousCallsHandler[obj.key]);
_this.continuousCallsHandler[obj.key] = null;
} else {
obj.down();
}
},
add: function(options) {
options = $.extend( {}, _this.defaults, options );
_this.controller[options.key] = options;
},
};