From 1db94e4ea3e72aee6f61e3ef98bab8ebacd6a02d Mon Sep 17 00:00:00 2001 From: Beau West Date: Tue, 17 May 2016 10:46:04 -0500 Subject: [PATCH 1/8] Created a `normalize_caps_lock` option Do not allow turning caps lock on to prevent combos from being activated. --- keypress.coffee | 45 +++++++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/keypress.coffee b/keypress.coffee index 135d104..af9ecf5 100644 --- a/keypress.coffee +++ b/keypress.coffee @@ -21,19 +21,20 @@ version 2.1.3 ### Combo options available and their defaults: - keys : [] - An array of the keys pressed together to activate combo. - count : 0 - The number of times a counting combo has been pressed. Reset on release. - is_unordered : false - Unless this is set to true, the keys can be pressed down in any order. - is_counting : false - Makes this a counting combo (see documentation). - is_exclusive : false - This combo will replace other exclusive combos when true. - is_solitary : false - This combo will only fire if ONLY it's keys are pressed down. - is_sequence : false - Rather than a key combo, this is an ordered key sequence. - prevent_default : false - Prevent default behavior for all component key keypresses. - prevent_repeat : false - Prevent the combo from repeating when keydown is held. - on_keydown : null - A function that is called when the combo is pressed. - on_keyup : null - A function that is called when the combo is released. - on_release : null - A function that is called when all keys in the combo are released. - this : undefined - Defines the scope for your callback functions. + keys : [] - An array of the keys pressed together to activate combo. + count : 0 - The number of times a counting combo has been pressed. Reset on release. + is_unordered : false - Unless this is set to true, the keys can be pressed down in any order. + is_counting : false - Makes this a counting combo (see documentation). + is_exclusive : false - This combo will replace other exclusive combos when true. + is_solitary : false - This combo will only fire if ONLY it's keys are pressed down. + is_sequence : false - Rather than a key combo, this is an ordered key sequence. + prevent_default : false - Prevent default behavior for all component key keypresses. + prevent_repeat : false - Prevent the combo from repeating when keydown is held. + normalize_caps_lock : false - Do not allow turning caps lock on to prevent combos from being activated. + on_keydown : null - A function that is called when the combo is pressed. + on_keyup : null - A function that is called when the combo is released. + on_release : null - A function that is called when all keys in the combo are released. + this : undefined - Defines the scope for your callback functions. ### ########### @@ -41,12 +42,13 @@ Combo options available and their defaults: ########### _factory_defaults = - is_unordered : false - is_counting : false - is_exclusive : false - is_solitary : false - prevent_default : false - prevent_repeat : false + is_unordered : false + is_counting : false + is_exclusive : false + is_solitary : false + prevent_default : false + prevent_repeat : false + normalize_caps_lock : false _modifier_keys = ["meta", "alt", "option", "ctrl", "shift", "cmd"] @@ -359,7 +361,10 @@ class keypress.Listener _match_combo_arrays: (potential_match, match_handler) -> # This will return all combos that match for source_combo in @_registered_combos - if (not source_combo.is_unordered and _compare_arrays_sorted(potential_match, source_combo.keys)) or (source_combo.is_unordered and _compare_arrays(potential_match, source_combo.keys)) + combo_potential_match = potential_match.slice(0); + if (source_combo.normalize_caps_lock and "caps" in combo_potential_match) + combo_potential_match.splice(combo_potential_match.indexOf("caps"), 1) + if (not source_combo.is_unordered and _compare_arrays_sorted(combo_potential_match, source_combo.keys)) or (source_combo.is_unordered and _compare_arrays(combo_potential_match, source_combo.keys)) match_handler source_combo return From 06f3a5de0e20938c364a8f5851092050d2247a3d Mon Sep 17 00:00:00 2001 From: Zach Walker Date: Thu, 14 Jul 2016 14:38:54 -0700 Subject: [PATCH 2/8] zw - recompling from cofee as js seems out of date --- keypress-2.1.4.min.js | 56 +++++++++++++++++++++---------------------- keypress.js | 38 ++++++++++++++++------------- 2 files changed, 50 insertions(+), 44 deletions(-) diff --git a/keypress-2.1.4.min.js b/keypress-2.1.4.min.js index 8bff56b..c6ddd88 100644 --- a/keypress-2.1.4.min.js +++ b/keypress-2.1.4.min.js @@ -3,31 +3,31 @@ Licensed under the Apache License, Version 2.0 http://www.apache.org/licenses/LICENSE-2.0 */ -(function(){var m,v,y,z,A,r,w,B,F,C,G,H,q,s,p,o,t,D,I,E={}.hasOwnProperty,j=[].indexOf||function(a){for(var c=0,b=this.length;cj.call(a,"cmd")?!1:!0};g.prototype._prevent_default=function(a,c){if((c||this.should_suppress_event_defaults)&&!this.should_force_event_defaults)if(a.preventDefault?a.preventDefault():a.returnValue=!1,a.stopPropagation)return a.stopPropagation()}; -g.prototype._get_active_combos=function(a){var c,b;c=[];b=w(this._keys_down,function(b){return b!==a});b.push(a);this._match_combo_arrays(b,function(a){return function(b){if(a._cmd_bug_check(b.keys))return c.push(b)}}(this));this._fuzzy_match_combo_arrays(b,function(a){return function(b){if(!(0<=j.call(c,b))&&!b.is_solitary&&a._cmd_bug_check(b.keys))return c.push(b)}}(this));return c};g.prototype._get_potential_combos=function(a){var c,b,d,e,f;b=[];f=this._registered_combos;d=0;for(e=f.length;dk;e=0<=k?++i:--i)if((c=this._active_combos[e])&&c.is_exclusive&&a.is_exclusive){c=c.keys;if(!h){g=0;for(n=c.length;gj.call(a.keys,b)){h=!1;break}}if(f&&!h){l=a.keys;g=0;for(n= -l.length;gj.call(c,b)){f=!0;break}}h&&(d?(c=this._active_combos.splice(e,1)[0],null!=c&&c.reset()):(c=this._active_combos.splice(e,1,a)[0],null!=c&&c.reset(),d=!0),f=!1)}}f&&this._active_combos.unshift(a);return h||f};g.prototype._remove_from_active_combos=function(a){var c,b,d,e;b=d=0;for(e=this._active_combos.length;0<=e?de;b=0<=e?++d:--d)if(c=this._active_combos[b],c===a){a=this._active_combos.splice(b,1)[0];a.reset();break}};g.prototype._get_possible_sequences=function(){var a, -c,b,d,e,f,h,i,g,n,k,l;d=[];n=this._registered_combos;f=0;for(g=n.length;f=k;c=1<=k?++h:--h)if(e=this._sequence.slice(-c),a.is_sequence){if(0>j.call(a.keys,"shift")&&(e=w(e,function(a){return"shift"!==a}),!e.length))continue;c=i=0;for(l=e.length;0<=l?il;c=0<=l?++i:--i)if(a.keys[c]===e[c])b=!0;else{b=!1;break}b&&d.push(a)}}return d};g.prototype._add_key_to_sequence=function(a,c){var b,d,e,f;this._sequence.push(a);d=this._get_possible_sequences(); -if(d.length){e=0;for(f=d.length;e=l;b=1<=l?++i:--i)if(f=w(this._sequence, -function(a){return 0<=j.call(c.keys,"shift")?!0:"shift"!==a}).slice(-b),c.keys.length===f.length){b=g=0;for(u=f.length;0<=u?gu;b=0<=u?++g:--g)if(e=f[b],!(0>j.call(c.keys,"shift")&&"shift"===e)&&!("shift"===a&&0>j.call(c.keys,"shift")))if(c.keys[b]===e)d=!0;else{d=!1;break}}if(d)return c.is_exclusive&&(this._sequence=[]),c}return!1};g.prototype._receive_input=function(a,c){var b,d;if(this._prevent_capture)this._keys_down.length&&(this._keys_down=[]);else if(b=z(null!=(d=a.keyCode)?d:a.key),(c|| -this._keys_down.length||!("alt"===b||b===o))&&b)return c?this._key_down(b,a):this._key_up(b,a)};g.prototype._fire=function(a,c,b,d){"function"===typeof c["on_"+a]&&this._prevent_default(b,!0!==c["on_"+a].call(c["this"],b,c.count,d));"release"===a&&(c.count=0);if("keyup"===a)return c.keyup_fired=!0};g.prototype._match_combo_arrays=function(a,c){var b,d,e,f;f=this._registered_combos;d=0;for(e=f.length;df;b=0<=f?++d:--d)this._keys_down[b]===e&&this._keys_down.splice(b,1)}d=this._get_active_combos(a);e=this._get_potential_combos(a);f=0;for(h=d.length;fj.call(this._keys_down,a)&&this._keys_down.push(a)}; -g.prototype._handle_combo_down=function(a,c,b,d){var e,f,h,g,m;if(0>j.call(a.keys,b))return!1;this._prevent_default(d,a&&a.prevent_default);e=!1;if(0<=j.call(this._keys_down,b)&&(e=!0,!a.allows_key_repeat()))return!1;h=this._add_to_active_combos(a,b);b=a.keyup_fired=!1;if(a.is_exclusive){g=0;for(m=c.length;ga.keys.length){b=!0;break}}if(!b&&(a.is_counting&&"function"===typeof a.on_keydown&&(a.count+=1),h))return this._fire("keydown",a,d,e)};g.prototype._key_up= -function(a,c){var b,d,e,f,h,g;b=a;(e=A(a,c))&&(a=e);e=s[b];c.shiftKey?e&&0<=j.call(this._keys_down,e)||(a=b):b&&0<=j.call(this._keys_down,b)||(a=e);(f=this._get_sequence(a))&&this._fire("keyup",f,c);if(0>j.call(this._keys_down,a))return!1;f=h=0;for(g=this._keys_down.length;0<=g?hg;f=0<=g?++h:--h)if((d=this._keys_down[f])===a||d===e||d===b){this._keys_down.splice(f,1);break}d=this._active_combos.length;e=[];g=this._active_combos;f=0;for(h=g.length;fd;b=0<=d?++c:--c)if(a===i._registered_combos[b]){i._registered_combos.splice(b, -1);break}else e.push(void 0);return e};if(a instanceof x)return b(a);"string"===typeof a&&(a=a.split(" "));f=this._registered_combos;g=[];d=0;for(e=f.length;de;b=0<=e?++d:--d)if(a[b]!==c[b])return!1;return!0};F=function(a,c){var b,d,e;d=0;for(e=a.length;dj.call(c,b))return!1;return!0};B=Array.prototype.indexOf||function(a,c){var b,d,e;b=d=0;for(e=a.length;0<=e?d<=e:d>=e;b=0<=e?++d:--d)if(a[b]===c)return b;return-1};C=function(a,c){var b,d,e,f;e=d=0;for(f=a.length;e=d)d=b;else return!1;return!0}; -p=function(){if(m.debug)return console.log.apply(console,arguments)};G=function(a){var c,b,d;c=!1;for(d in q)if(b=q[d],a===b){c=!0;break}if(!c)for(d in s)if(b=s[d],a===b){c=!0;break}return c};I=function(a){var c,b,d,e,f,g,i;f=!0;a.keys.length||p("You're trying to bind a combo with no keys:",a);b=g=0;for(i=a.keys.length;0<=i?gi;b=0<=i?++g:--g)d=a.keys[b],(c=H[d])&&(d=a.keys[b]=c),"meta"===d&&a.keys.splice(b,1,o),"cmd"===d&&p('Warning: use the "meta" key rather than "cmd" for Windows compatibility'); -i=a.keys;c=0;for(g=i.length;c",",":"<","'":'"',";":":","[":"{","]":"}","\\":"|","`":"~","=":"+","-":"_",1:"!",2:"@",3:"#",4:"$",5:"%",6:"^",7:"&",8:"*",9:"(","0":")"};q={"0":"\\",8:"backspace",9:"tab",12:"num",13:"enter",16:"shift", -17:"ctrl",18:"alt",19:"pause",20:"caps",27:"esc",32:"space",33:"pageup",34:"pagedown",35:"end",36:"home",37:"left",38:"up",39:"right",40:"down",44:"print",45:"insert",46:"delete",48:"0",49:"1",50:"2",51:"3",52:"4",53:"5",54:"6",55:"7",56:"8",57:"9",65:"a",66:"b",67:"c",68:"d",69:"e",70:"f",71:"g",72:"h",73:"i",74:"j",75:"k",76:"l",77:"m",78:"n",79:"o",80:"p",81:"q",82:"r",83:"s",84:"t",85:"u",86:"v",87:"w",88:"x",89:"y",90:"z",91:"cmd",92:"cmd",93:"cmd",96:"num_0",97:"num_1",98:"num_2",99:"num_3", -100:"num_4",101:"num_5",102:"num_6",103:"num_7",104:"num_8",105:"num_9",106:"num_multiply",107:"num_add",108:"num_enter",109:"num_subtract",110:"num_decimal",111:"num_divide",112:"f1",113:"f2",114:"f3",115:"f4",116:"f5",117:"f6",118:"f7",119:"f8",120:"f9",121:"f10",122:"f11",123:"f12",124:"print",144:"num",145:"scroll",186:";",187:"=",188:",",189:"-",190:".",191:"/",192:"`",219:"[",220:"\\",221:"]",222:"'",223:"`",224:"cmd",225:"alt",57392:"ctrl",63289:"num",59:";",61:"=",173:"-"};m._keycode_dictionary= -q;m._is_array_in_array_sorted=C;-1!==navigator.userAgent.indexOf("Mac OS X")&&(o="cmd");-1!==navigator.userAgent.indexOf("Opera")&&(q["17"]="cmd");"function"===typeof define&&define.amd?define([],function(){return m}):"undefined"!==typeof exports&&null!==exports?exports.keypress=m:window.keypress=m}).call(this); +(function(){var m,v,y,z,A,r,w,B,F,C,G,H,q,s,p,o,t,D,I,E={}.hasOwnProperty,j=[].indexOf||function(a){for(var c=0,b=this.length;cj.call(a,"cmd")?!1:!0};g.prototype._prevent_default=function(a,c){if((c||this.should_suppress_event_defaults)&&!this.should_force_event_defaults)if(a.preventDefault?a.preventDefault(): +a.returnValue=!1,a.stopPropagation)return a.stopPropagation()};g.prototype._get_active_combos=function(a){var c,b;c=[];b=w(this._keys_down,function(b){return b!==a});b.push(a);this._match_combo_arrays(b,function(a){return function(b){if(a._cmd_bug_check(b.keys))return c.push(b)}}(this));this._fuzzy_match_combo_arrays(b,function(a){return function(b){if(!(0<=j.call(c,b))&&!b.is_solitary&&a._cmd_bug_check(b.keys))return c.push(b)}}(this));return c};g.prototype._get_potential_combos=function(a){var c, +b,d,e,f;b=[];f=this._registered_combos;d=0;for(e=f.length;dk;e=0<=k?++i:--i)if((c=this._active_combos[e])&&c.is_exclusive&&a.is_exclusive){c=c.keys;if(!h){g=0;for(n=c.length;gj.call(a.keys,b)){h=!1;break}}if(f&&!h){l=a.keys;g=0;for(n=l.length;gj.call(c,b)){f=!0;break}}h&&(d?(c=this._active_combos.splice(e,1)[0],null!=c&&c.reset()):(c=this._active_combos.splice(e,1,a)[0],null!=c&&c.reset(),d=!0),f=!1)}}f&&this._active_combos.unshift(a);return h||f};g.prototype._remove_from_active_combos=function(a){var c,b,d,e;b=d=0;for(e=this._active_combos.length;0<=e?de;b=0<=e?++d:--d)if(c=this._active_combos[b],c===a){a=this._active_combos.splice(b, +1)[0];a.reset();break}};g.prototype._get_possible_sequences=function(){var a,c,b,d,e,f,h,i,g,n,k,l;d=[];n=this._registered_combos;f=0;for(g=n.length;f=k;c=1<=k?++h:--h)if(e=this._sequence.slice(-c),a.is_sequence){if(0>j.call(a.keys,"shift")&&(e=w(e,function(a){return"shift"!==a}),!e.length))continue;c=i=0;for(l=e.length;0<=l?il;c=0<=l?++i:--i)if(a.keys[c]===e[c])b=!0;else{b=!1;break}b&&d.push(a)}}return d};g.prototype._add_key_to_sequence= +function(a,c){var b,d,e,f;this._sequence.push(a);d=this._get_possible_sequences();if(d.length){e=0;for(f=d.length;e=l;b=1<=l?++i:--i)if(f=w(this._sequence,function(a){return 0<=j.call(c.keys,"shift")?!0:"shift"!==a}).slice(-b),c.keys.length===f.length){b=g=0;for(u=f.length;0<=u?gu;b=0<=u?++g:--g)if(e=f[b],!(0>j.call(c.keys,"shift")&&"shift"===e)&&!("shift"===a&&0>j.call(c.keys,"shift")))if(c.keys[b]===e)d=!0;else{d=!1;break}}if(d)return c.is_exclusive&&(this._sequence=[]),c}return!1};g.prototype._receive_input=function(a,c){var b,d;if(this._prevent_capture)this._keys_down.length&& +(this._keys_down=[]);else if(b=z(null!=(d=a.keyCode)?d:a.key),(c||this._keys_down.length||!("alt"===b||b===o))&&b)return c?this._key_down(b,a):this._key_up(b,a)};g.prototype._fire=function(a,c,b,d){"function"===typeof c["on_"+a]&&this._prevent_default(b,!0!==c["on_"+a].call(c["this"],b,c.count,d));"release"===a&&(c.count=0);if("keyup"===a)return c.keyup_fired=!0};g.prototype._match_combo_arrays=function(a,c){var b,d,e,f,h;h=this._registered_combos;e=0;for(f=h.length;ef;b=0<=f?++d:--d)this._keys_down[b]===e&&this._keys_down.splice(b,1)}d=this._get_active_combos(a);e=this._get_potential_combos(a);f=0;for(h=d.length;fj.call(this._keys_down,a)&&this._keys_down.push(a)};g.prototype._handle_combo_down=function(a,c,b,d){var e,f,h,g,m;if(0>j.call(a.keys,b))return!1;this._prevent_default(d,a&&a.prevent_default);e=!1;if(0<=j.call(this._keys_down,b)&&(e=!0,!a.allows_key_repeat()))return!1;h=this._add_to_active_combos(a,b);b=a.keyup_fired=!1;if(a.is_exclusive){g=0;for(m=c.length;g +a.keys.length){b=!0;break}}if(!b&&(a.is_counting&&"function"===typeof a.on_keydown&&(a.count+=1),h))return this._fire("keydown",a,d,e)};g.prototype._key_up=function(a,c){var b,d,e,f,h,g;b=a;(e=A(a,c))&&(a=e);e=s[b];c.shiftKey?e&&0<=j.call(this._keys_down,e)||(a=b):b&&0<=j.call(this._keys_down,b)||(a=e);(f=this._get_sequence(a))&&this._fire("keyup",f,c);if(0>j.call(this._keys_down,a))return!1;f=h=0;for(g=this._keys_down.length;0<=g?hg;f=0<=g?++h:--h)if((d=this._keys_down[f])===a||d===e||d===b){this._keys_down.splice(f, +1);break}d=this._active_combos.length;e=[];g=this._active_combos;f=0;for(h=g.length;fd;b=0<=d?++c:--c)if(a===i._registered_combos[b]){i._registered_combos.splice(b,1);break}else e.push(void 0);return e};if(a instanceof x)return b(a);"string"===typeof a&&(a=a.split(" "));f=this._registered_combos;g=[];d=0;for(e=f.length;de;b=0<=e?++d:--d)if(a[b]!==c[b])return!1;return!0};F=function(a,c){var b,d,e;d=0;for(e=a.length;dj.call(c,b))return!1;return!0};B=Array.prototype.indexOf||function(a,c){var b,d,e;b=d=0;for(e=a.length;0<=e?d<=e:d>=e;b=0<=e?++d:--d)if(a[b]=== +c)return b;return-1};C=function(a,c){var b,d,e,f;e=d=0;for(f=a.length;e=d)d=b;else return!1;return!0};p=function(){if(m.debug)return console.log.apply(console,arguments)};G=function(a){var c,b,d;c=!1;for(d in q)if(b=q[d],a===b){c=!0;break}if(!c)for(d in s)if(b=s[d],a===b){c=!0;break}return c};I=function(a){var c,b,d,e,f,g,i;f=!0;a.keys.length||p("You're trying to bind a combo with no keys:",a);b=g=0;for(i=a.keys.length;0<=i?gi;b=0<=i?++g:--g)d=a.keys[b],(c=H[d])&& +(d=a.keys[b]=c),"meta"===d&&a.keys.splice(b,1,o),"cmd"===d&&p('Warning: use the "meta" key rather than "cmd" for Windows compatibility');i=a.keys;c=0;for(g=i.length;c",",":"<","'":'"',";":":","[":"{","]":"}","\\":"|","`":"~","=":"+","-":"_", +1:"!",2:"@",3:"#",4:"$",5:"%",6:"^",7:"&",8:"*",9:"(","0":")"};q={"0":"\\",8:"backspace",9:"tab",12:"num",13:"enter",16:"shift",17:"ctrl",18:"alt",19:"pause",20:"caps",27:"esc",32:"space",33:"pageup",34:"pagedown",35:"end",36:"home",37:"left",38:"up",39:"right",40:"down",44:"print",45:"insert",46:"delete",48:"0",49:"1",50:"2",51:"3",52:"4",53:"5",54:"6",55:"7",56:"8",57:"9",65:"a",66:"b",67:"c",68:"d",69:"e",70:"f",71:"g",72:"h",73:"i",74:"j",75:"k",76:"l",77:"m",78:"n",79:"o",80:"p",81:"q",82:"r", +83:"s",84:"t",85:"u",86:"v",87:"w",88:"x",89:"y",90:"z",91:"cmd",92:"cmd",93:"cmd",96:"num_0",97:"num_1",98:"num_2",99:"num_3",100:"num_4",101:"num_5",102:"num_6",103:"num_7",104:"num_8",105:"num_9",106:"num_multiply",107:"num_add",108:"num_enter",109:"num_subtract",110:"num_decimal",111:"num_divide",112:"f1",113:"f2",114:"f3",115:"f4",116:"f5",117:"f6",118:"f7",119:"f8",120:"f9",121:"f10",122:"f11",123:"f12",124:"print",144:"num",145:"scroll",186:";",187:"=",188:",",189:"-",190:".",191:"/",192:"`", +219:"[",220:"\\",221:"]",222:"'",223:"`",224:"cmd",225:"alt",57392:"ctrl",63289:"num",59:";",61:"=",173:"-"};m._keycode_dictionary=q;m._is_array_in_array_sorted=C;-1!==navigator.userAgent.indexOf("Mac OS X")&&(o="cmd");-1!==navigator.userAgent.indexOf("Opera")&&(q["17"]="cmd");"function"===typeof define&&define.amd?define([],function(){return m}):"undefined"!==typeof exports&&null!==exports?exports.keypress=m:window.keypress=m}).call(this); diff --git a/keypress.js b/keypress.js index 8913d02..9005cb5 100644 --- a/keypress.js +++ b/keypress.js @@ -24,19 +24,20 @@ version 2.1.3 /* Combo options available and their defaults: - keys : [] - An array of the keys pressed together to activate combo. - count : 0 - The number of times a counting combo has been pressed. Reset on release. - is_unordered : false - Unless this is set to true, the keys can be pressed down in any order. - is_counting : false - Makes this a counting combo (see documentation). - is_exclusive : false - This combo will replace other exclusive combos when true. - is_solitary : false - This combo will only fire if ONLY it's keys are pressed down. - is_sequence : false - Rather than a key combo, this is an ordered key sequence. - prevent_default : false - Prevent default behavior for all component key keypresses. - prevent_repeat : false - Prevent the combo from repeating when keydown is held. - on_keydown : null - A function that is called when the combo is pressed. - on_keyup : null - A function that is called when the combo is released. - on_release : null - A function that is called when all keys in the combo are released. - this : undefined - Defines the scope for your callback functions. + keys : [] - An array of the keys pressed together to activate combo. + count : 0 - The number of times a counting combo has been pressed. Reset on release. + is_unordered : false - Unless this is set to true, the keys can be pressed down in any order. + is_counting : false - Makes this a counting combo (see documentation). + is_exclusive : false - This combo will replace other exclusive combos when true. + is_solitary : false - This combo will only fire if ONLY it's keys are pressed down. + is_sequence : false - Rather than a key combo, this is an ordered key sequence. + prevent_default : false - Prevent default behavior for all component key keypresses. + prevent_repeat : false - Prevent the combo from repeating when keydown is held. + normalize_caps_lock : false - Do not allow turning caps lock on to prevent combos from being activated. + on_keydown : null - A function that is called when the combo is pressed. + on_keyup : null - A function that is called when the combo is released. + on_release : null - A function that is called when all keys in the combo are released. + this : undefined - Defines the scope for your callback functions. */ (function() { @@ -50,7 +51,8 @@ Combo options available and their defaults: is_exclusive: false, is_solitary: false, prevent_default: false, - prevent_repeat: false + prevent_repeat: false, + normalize_caps_lock: false }; _modifier_keys = ["meta", "alt", "option", "ctrl", "shift", "cmd"]; @@ -439,11 +441,15 @@ Combo options available and their defaults: }; Listener.prototype._match_combo_arrays = function(potential_match, match_handler) { - var source_combo, _i, _len, _ref; + var combo_potential_match, source_combo, _i, _len, _ref; _ref = this._registered_combos; for (_i = 0, _len = _ref.length; _i < _len; _i++) { source_combo = _ref[_i]; - if ((!source_combo.is_unordered && _compare_arrays_sorted(potential_match, source_combo.keys)) || (source_combo.is_unordered && _compare_arrays(potential_match, source_combo.keys))) { + combo_potential_match = potential_match.slice(0); + if (source_combo.normalize_caps_lock && __indexOf.call(combo_potential_match, "caps") >= 0) { + combo_potential_match.splice(combo_potential_match.indexOf("caps"), 1); + } + if ((!source_combo.is_unordered && _compare_arrays_sorted(combo_potential_match, source_combo.keys)) || (source_combo.is_unordered && _compare_arrays(combo_potential_match, source_combo.keys))) { match_handler(source_combo); } } From 91676d9158161d17afd77926e4db702cd7c84fdb Mon Sep 17 00:00:00 2001 From: Zach Walker Date: Thu, 14 Jul 2016 14:47:55 -0700 Subject: [PATCH 3/8] zw - sequence is now cleared on timer because scope of this is set correctly in closure --- keypress-2.1.4.min.js | 4 +- keypress.coffee | 2 +- keypress.js | 1148 ----------------------------------------- 3 files changed, 3 insertions(+), 1151 deletions(-) delete mode 100644 keypress.js diff --git a/keypress-2.1.4.min.js b/keypress-2.1.4.min.js index c6ddd88..90c4e9e 100644 --- a/keypress-2.1.4.min.js +++ b/keypress-2.1.4.min.js @@ -12,8 +12,8 @@ a.returnValue=!1,a.stopPropagation)return a.stopPropagation()};g.prototype._get_ b,d,e,f;b=[];f=this._registered_combos;d=0;for(e=f.length;dk;e=0<=k?++i:--i)if((c=this._active_combos[e])&&c.is_exclusive&&a.is_exclusive){c=c.keys;if(!h){g=0;for(n=c.length;gj.call(a.keys,b)){h=!1;break}}if(f&&!h){l=a.keys;g=0;for(n=l.length;gj.call(c,b)){f=!0;break}}h&&(d?(c=this._active_combos.splice(e,1)[0],null!=c&&c.reset()):(c=this._active_combos.splice(e,1,a)[0],null!=c&&c.reset(),d=!0),f=!1)}}f&&this._active_combos.unshift(a);return h||f};g.prototype._remove_from_active_combos=function(a){var c,b,d,e;b=d=0;for(e=this._active_combos.length;0<=e?de;b=0<=e?++d:--d)if(c=this._active_combos[b],c===a){a=this._active_combos.splice(b, 1)[0];a.reset();break}};g.prototype._get_possible_sequences=function(){var a,c,b,d,e,f,h,i,g,n,k,l;d=[];n=this._registered_combos;f=0;for(g=n.length;f=k;c=1<=k?++h:--h)if(e=this._sequence.slice(-c),a.is_sequence){if(0>j.call(a.keys,"shift")&&(e=w(e,function(a){return"shift"!==a}),!e.length))continue;c=i=0;for(l=e.length;0<=l?il;c=0<=l?++i:--i)if(a.keys[c]===e[c])b=!0;else{b=!1;break}b&&d.push(a)}}return d};g.prototype._add_key_to_sequence= -function(a,c){var b,d,e,f;this._sequence.push(a);d=this._get_possible_sequences();if(d.length){e=0;for(f=d.length;e=l;b=1<=l?++i:--i)if(f=w(this._sequence,function(a){return 0<=j.call(c.keys,"shift")?!0:"shift"!==a}).slice(-b),c.keys.length===f.length){b=g=0;for(u=f.length;0<=u?gu;b=0<=u?++g:--g)if(e=f[b],!(0>j.call(c.keys,"shift")&&"shift"===e)&&!("shift"===a&&0>j.call(c.keys,"shift")))if(c.keys[b]===e)d=!0;else{d=!1;break}}if(d)return c.is_exclusive&&(this._sequence=[]),c}return!1};g.prototype._receive_input=function(a,c){var b,d;if(this._prevent_capture)this._keys_down.length&& +function(a,c){var b,d,e,f;this._sequence.push(a);d=this._get_possible_sequences();if(d.length){e=0;for(f=d.length;e=l;b=1<=l?++i:--i)if(f=w(this._sequence,function(a){return 0<=j.call(c.keys,"shift")?!0:"shift"!==a}).slice(-b),c.keys.length===f.length){b=g=0;for(u=f.length;0<=u?gu;b=0<=u?++g:--g)if(e=f[b],!(0>j.call(c.keys,"shift")&&"shift"===e)&&!("shift"===a&&0>j.call(c.keys,"shift")))if(c.keys[b]===e)d=!0;else{d=!1;break}}if(d)return c.is_exclusive&&(this._sequence=[]),c}return!1};g.prototype._receive_input=function(a,c){var b,d;if(this._prevent_capture)this._keys_down.length&& (this._keys_down=[]);else if(b=z(null!=(d=a.keyCode)?d:a.key),(c||this._keys_down.length||!("alt"===b||b===o))&&b)return c?this._key_down(b,a):this._key_up(b,a)};g.prototype._fire=function(a,c,b,d){"function"===typeof c["on_"+a]&&this._prevent_default(b,!0!==c["on_"+a].call(c["this"],b,c.count,d));"release"===a&&(c.count=0);if("keyup"===a)return c.keyup_fired=!0};g.prototype._match_combo_arrays=function(a,c){var b,d,e,f,h;h=this._registered_combos;e=0;for(f=h.length;ef;b=0<=f?++d:--d)this._keys_down[b]===e&&this._keys_down.splice(b,1)}d=this._get_active_combos(a);e=this._get_potential_combos(a);f=0;for(h=d.length;f -1 - @_sequence_timer = setTimeout -> + @_sequence_timer = setTimeout => @_sequence = [] , @sequence_delay else diff --git a/keypress.js b/keypress.js deleted file mode 100644 index 9005cb5..0000000 --- a/keypress.js +++ /dev/null @@ -1,1148 +0,0 @@ -// Generated by CoffeeScript 1.8.0 - -/* -Copyright 2014 David Mauro - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - -http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. - -Keypress is a robust keyboard input capturing Javascript utility -focused on input for games. - -version 2.1.3 - */ - - -/* -Combo options available and their defaults: - keys : [] - An array of the keys pressed together to activate combo. - count : 0 - The number of times a counting combo has been pressed. Reset on release. - is_unordered : false - Unless this is set to true, the keys can be pressed down in any order. - is_counting : false - Makes this a counting combo (see documentation). - is_exclusive : false - This combo will replace other exclusive combos when true. - is_solitary : false - This combo will only fire if ONLY it's keys are pressed down. - is_sequence : false - Rather than a key combo, this is an ordered key sequence. - prevent_default : false - Prevent default behavior for all component key keypresses. - prevent_repeat : false - Prevent the combo from repeating when keydown is held. - normalize_caps_lock : false - Do not allow turning caps lock on to prevent combos from being activated. - on_keydown : null - A function that is called when the combo is pressed. - on_keyup : null - A function that is called when the combo is released. - on_release : null - A function that is called when all keys in the combo are released. - this : undefined - Defines the scope for your callback functions. - */ - -(function() { - var Combo, keypress, _change_keycodes_by_browser, _compare_arrays, _compare_arrays_sorted, _convert_key_to_readable, _convert_to_shifted_key, _decide_meta_key, _factory_defaults, _filter_array, _index_of_in_array, _is_array_in_array, _is_array_in_array_sorted, _key_is_valid, _keycode_alternate_names, _keycode_dictionary, _keycode_shifted_keys, _log_error, _metakey, _modifier_event_mapping, _modifier_keys, _validate_combo, - __hasProp = {}.hasOwnProperty, - __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; - - _factory_defaults = { - is_unordered: false, - is_counting: false, - is_exclusive: false, - is_solitary: false, - prevent_default: false, - prevent_repeat: false, - normalize_caps_lock: false - }; - - _modifier_keys = ["meta", "alt", "option", "ctrl", "shift", "cmd"]; - - _metakey = "ctrl"; - - keypress = {}; - - keypress.debug = false; - - Combo = (function() { - function Combo(dictionary) { - var property, value; - for (property in dictionary) { - if (!__hasProp.call(dictionary, property)) continue; - value = dictionary[property]; - if (value !== false) { - this[property] = value; - } - } - this.keys = this.keys || []; - this.count = this.count || 0; - } - - Combo.prototype.allows_key_repeat = function() { - return !this.prevent_repeat && typeof this.on_keydown === "function"; - }; - - Combo.prototype.reset = function() { - this.count = 0; - return this.keyup_fired = null; - }; - - return Combo; - - })(); - - keypress.Listener = (function() { - function Listener(element, defaults) { - var attach_handler, property, value; - if ((typeof jQuery !== "undefined" && jQuery !== null) && element instanceof jQuery) { - if (element.length !== 1) { - _log_error("Warning: your jQuery selector should have exactly one object."); - } - element = element[0]; - } - this.should_suppress_event_defaults = false; - this.should_force_event_defaults = false; - this.sequence_delay = 800; - this._registered_combos = []; - this._keys_down = []; - this._active_combos = []; - this._sequence = []; - this._sequence_timer = null; - this._prevent_capture = false; - this._defaults = defaults || {}; - for (property in _factory_defaults) { - if (!__hasProp.call(_factory_defaults, property)) continue; - value = _factory_defaults[property]; - this._defaults[property] = this._defaults[property] || value; - } - this.element = element || document.body; - attach_handler = function(target, event, handler) { - if (target.addEventListener) { - target.addEventListener(event, handler); - } else if (target.attachEvent) { - target.attachEvent("on" + event, handler); - } - return handler; - }; - this.keydown_event = attach_handler(this.element, "keydown", (function(_this) { - return function(e) { - e = e || window.event; - _this._receive_input(e, true); - return _this._bug_catcher(e); - }; - })(this)); - this.keyup_event = attach_handler(this.element, "keyup", (function(_this) { - return function(e) { - e = e || window.event; - return _this._receive_input(e, false); - }; - })(this)); - this.blur_event = attach_handler(window, "blur", (function(_this) { - return function() { - var key, _i, _len, _ref; - _ref = _this._keys_down; - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - key = _ref[_i]; - _this._key_up(key, {}); - } - return _this._keys_down = []; - }; - })(this)); - } - - Listener.prototype.destroy = function() { - var remove_handler; - remove_handler = function(target, event, handler) { - if (target.removeEventListener != null) { - return target.removeEventListener(event, handler); - } else if (target.removeEvent != null) { - return target.removeEvent("on" + event, handler); - } - }; - remove_handler(this.element, "keydown", this.keydown_event); - remove_handler(this.element, "keyup", this.keyup_event); - return remove_handler(window, "blur", this.blur_event); - }; - - Listener.prototype._bug_catcher = function(e) { - var _ref, _ref1; - if (_metakey === "cmd" && __indexOf.call(this._keys_down, "cmd") >= 0 && ((_ref = _convert_key_to_readable((_ref1 = e.keyCode) != null ? _ref1 : e.key)) !== "cmd" && _ref !== "shift" && _ref !== "alt" && _ref !== "caps" && _ref !== "tab")) { - return this._receive_input(e, false); - } - }; - - Listener.prototype._cmd_bug_check = function(combo_keys) { - if (_metakey === "cmd" && __indexOf.call(this._keys_down, "cmd") >= 0 && __indexOf.call(combo_keys, "cmd") < 0) { - return false; - } - return true; - }; - - Listener.prototype._prevent_default = function(e, should_prevent) { - if ((should_prevent || this.should_suppress_event_defaults) && !this.should_force_event_defaults) { - if (e.preventDefault) { - e.preventDefault(); - } else { - e.returnValue = false; - } - if (e.stopPropagation) { - return e.stopPropagation(); - } - } - }; - - Listener.prototype._get_active_combos = function(key) { - var active_combos, keys_down; - active_combos = []; - keys_down = _filter_array(this._keys_down, function(down_key) { - return down_key !== key; - }); - keys_down.push(key); - this._match_combo_arrays(keys_down, (function(_this) { - return function(match) { - if (_this._cmd_bug_check(match.keys)) { - return active_combos.push(match); - } - }; - })(this)); - this._fuzzy_match_combo_arrays(keys_down, (function(_this) { - return function(match) { - if (__indexOf.call(active_combos, match) >= 0) { - return; - } - if (!(match.is_solitary || !_this._cmd_bug_check(match.keys))) { - return active_combos.push(match); - } - }; - })(this)); - return active_combos; - }; - - Listener.prototype._get_potential_combos = function(key) { - var combo, potentials, _i, _len, _ref; - potentials = []; - _ref = this._registered_combos; - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - combo = _ref[_i]; - if (combo.is_sequence) { - continue; - } - if (__indexOf.call(combo.keys, key) >= 0 && this._cmd_bug_check(combo.keys)) { - potentials.push(combo); - } - } - return potentials; - }; - - Listener.prototype._add_to_active_combos = function(combo) { - var active_combo, active_key, active_keys, already_replaced, combo_key, i, should_prepend, should_replace, _i, _j, _k, _len, _len1, _ref, _ref1; - should_replace = false; - should_prepend = true; - already_replaced = false; - if (__indexOf.call(this._active_combos, combo) >= 0) { - return true; - } else if (this._active_combos.length) { - for (i = _i = 0, _ref = this._active_combos.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) { - active_combo = this._active_combos[i]; - if (!(active_combo && active_combo.is_exclusive && combo.is_exclusive)) { - continue; - } - active_keys = active_combo.keys; - if (!should_replace) { - for (_j = 0, _len = active_keys.length; _j < _len; _j++) { - active_key = active_keys[_j]; - should_replace = true; - if (__indexOf.call(combo.keys, active_key) < 0) { - should_replace = false; - break; - } - } - } - if (should_prepend && !should_replace) { - _ref1 = combo.keys; - for (_k = 0, _len1 = _ref1.length; _k < _len1; _k++) { - combo_key = _ref1[_k]; - should_prepend = false; - if (__indexOf.call(active_keys, combo_key) < 0) { - should_prepend = true; - break; - } - } - } - if (should_replace) { - if (already_replaced) { - active_combo = this._active_combos.splice(i, 1)[0]; - if (active_combo != null) { - active_combo.reset(); - } - } else { - active_combo = this._active_combos.splice(i, 1, combo)[0]; - if (active_combo != null) { - active_combo.reset(); - } - already_replaced = true; - } - should_prepend = false; - } - } - } - if (should_prepend) { - this._active_combos.unshift(combo); - } - return should_replace || should_prepend; - }; - - Listener.prototype._remove_from_active_combos = function(combo) { - var active_combo, i, _i, _ref; - for (i = _i = 0, _ref = this._active_combos.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) { - active_combo = this._active_combos[i]; - if (active_combo === combo) { - combo = this._active_combos.splice(i, 1)[0]; - combo.reset(); - break; - } - } - }; - - Listener.prototype._get_possible_sequences = function() { - var combo, i, j, match, matches, sequence, _i, _j, _k, _len, _ref, _ref1, _ref2; - matches = []; - _ref = this._registered_combos; - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - combo = _ref[_i]; - for (j = _j = 1, _ref1 = this._sequence.length; 1 <= _ref1 ? _j <= _ref1 : _j >= _ref1; j = 1 <= _ref1 ? ++_j : --_j) { - sequence = this._sequence.slice(-j); - if (!combo.is_sequence) { - continue; - } - if (__indexOf.call(combo.keys, "shift") < 0) { - sequence = _filter_array(sequence, function(key) { - return key !== "shift"; - }); - if (!sequence.length) { - continue; - } - } - for (i = _k = 0, _ref2 = sequence.length; 0 <= _ref2 ? _k < _ref2 : _k > _ref2; i = 0 <= _ref2 ? ++_k : --_k) { - if (combo.keys[i] === sequence[i]) { - match = true; - } else { - match = false; - break; - } - } - if (match) { - matches.push(combo); - } - } - } - return matches; - }; - - Listener.prototype._add_key_to_sequence = function(key, e) { - var combo, sequence_combos, _i, _len; - this._sequence.push(key); - sequence_combos = this._get_possible_sequences(); - if (sequence_combos.length) { - for (_i = 0, _len = sequence_combos.length; _i < _len; _i++) { - combo = sequence_combos[_i]; - this._prevent_default(e, combo.prevent_default); - } - if (this._sequence_timer) { - clearTimeout(this._sequence_timer); - } - if (this.sequence_delay > -1) { - this._sequence_timer = setTimeout(function() { - return this._sequence = []; - }, this.sequence_delay); - } - } else { - this._sequence = []; - } - }; - - Listener.prototype._get_sequence = function(key) { - var combo, i, j, match, seq_key, sequence, _i, _j, _k, _len, _ref, _ref1, _ref2; - _ref = this._registered_combos; - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - combo = _ref[_i]; - if (!combo.is_sequence) { - continue; - } - for (j = _j = 1, _ref1 = this._sequence.length; 1 <= _ref1 ? _j <= _ref1 : _j >= _ref1; j = 1 <= _ref1 ? ++_j : --_j) { - sequence = (_filter_array(this._sequence, function(seq_key) { - if (__indexOf.call(combo.keys, "shift") >= 0) { - return true; - } - return seq_key !== "shift"; - })).slice(-j); - if (combo.keys.length !== sequence.length) { - continue; - } - for (i = _k = 0, _ref2 = sequence.length; 0 <= _ref2 ? _k < _ref2 : _k > _ref2; i = 0 <= _ref2 ? ++_k : --_k) { - seq_key = sequence[i]; - if (__indexOf.call(combo.keys, "shift") < 0) { - if (seq_key === "shift") { - continue; - } - } - if (key === "shift" && __indexOf.call(combo.keys, "shift") < 0) { - continue; - } - if (combo.keys[i] === seq_key) { - match = true; - } else { - match = false; - break; - } - } - } - if (match) { - if (combo.is_exclusive) { - this._sequence = []; - } - return combo; - } - } - return false; - }; - - Listener.prototype._receive_input = function(e, is_keydown) { - var key, _ref; - if (this._prevent_capture) { - if (this._keys_down.length) { - this._keys_down = []; - } - return; - } - key = _convert_key_to_readable((_ref = e.keyCode) != null ? _ref : e.key); - if (!is_keydown && !this._keys_down.length && (key === "alt" || key === _metakey)) { - return; - } - if (!key) { - return; - } - if (is_keydown) { - return this._key_down(key, e); - } else { - return this._key_up(key, e); - } - }; - - Listener.prototype._fire = function(event, combo, key_event, is_autorepeat) { - if (typeof combo["on_" + event] === "function") { - this._prevent_default(key_event, combo["on_" + event].call(combo["this"], key_event, combo.count, is_autorepeat) !== true); - } - if (event === "release") { - combo.count = 0; - } - if (event === "keyup") { - return combo.keyup_fired = true; - } - }; - - Listener.prototype._match_combo_arrays = function(potential_match, match_handler) { - var combo_potential_match, source_combo, _i, _len, _ref; - _ref = this._registered_combos; - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - source_combo = _ref[_i]; - combo_potential_match = potential_match.slice(0); - if (source_combo.normalize_caps_lock && __indexOf.call(combo_potential_match, "caps") >= 0) { - combo_potential_match.splice(combo_potential_match.indexOf("caps"), 1); - } - if ((!source_combo.is_unordered && _compare_arrays_sorted(combo_potential_match, source_combo.keys)) || (source_combo.is_unordered && _compare_arrays(combo_potential_match, source_combo.keys))) { - match_handler(source_combo); - } - } - }; - - Listener.prototype._fuzzy_match_combo_arrays = function(potential_match, match_handler) { - var source_combo, _i, _len, _ref; - _ref = this._registered_combos; - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - source_combo = _ref[_i]; - if ((!source_combo.is_unordered && _is_array_in_array_sorted(source_combo.keys, potential_match)) || (source_combo.is_unordered && _is_array_in_array(source_combo.keys, potential_match))) { - match_handler(source_combo); - } - } - }; - - Listener.prototype._keys_remain = function(combo) { - var key, keys_remain, _i, _len, _ref; - _ref = combo.keys; - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - key = _ref[_i]; - if (__indexOf.call(this._keys_down, key) >= 0) { - keys_remain = true; - break; - } - } - return keys_remain; - }; - - Listener.prototype._key_down = function(key, e) { - var combo, combos, event_mod, i, mod, potential, potential_combos, sequence_combo, shifted_key, _i, _j, _k, _len, _len1, _ref; - shifted_key = _convert_to_shifted_key(key, e); - if (shifted_key) { - key = shifted_key; - } - this._add_key_to_sequence(key, e); - sequence_combo = this._get_sequence(key); - if (sequence_combo) { - this._fire("keydown", sequence_combo, e); - } - for (mod in _modifier_event_mapping) { - event_mod = _modifier_event_mapping[mod]; - if (!e[event_mod]) { - continue; - } - if (mod === key || __indexOf.call(this._keys_down, mod) >= 0) { - continue; - } - this._keys_down.push(mod); - } - for (mod in _modifier_event_mapping) { - event_mod = _modifier_event_mapping[mod]; - if (mod === key) { - continue; - } - if (__indexOf.call(this._keys_down, mod) >= 0 && !e[event_mod]) { - if (mod === "cmd" && _metakey !== "cmd") { - continue; - } - for (i = _i = 0, _ref = this._keys_down.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) { - if (this._keys_down[i] === mod) { - this._keys_down.splice(i, 1); - } - } - } - } - combos = this._get_active_combos(key); - potential_combos = this._get_potential_combos(key); - for (_j = 0, _len = combos.length; _j < _len; _j++) { - combo = combos[_j]; - this._handle_combo_down(combo, potential_combos, key, e); - } - if (potential_combos.length) { - for (_k = 0, _len1 = potential_combos.length; _k < _len1; _k++) { - potential = potential_combos[_k]; - this._prevent_default(e, potential.prevent_default); - } - } - if (__indexOf.call(this._keys_down, key) < 0) { - this._keys_down.push(key); - } - }; - - Listener.prototype._handle_combo_down = function(combo, potential_combos, key, e) { - var is_autorepeat, is_other_exclusive, potential_combo, result, _i, _len; - if (__indexOf.call(combo.keys, key) < 0) { - return false; - } - this._prevent_default(e, combo && combo.prevent_default); - is_autorepeat = false; - if (__indexOf.call(this._keys_down, key) >= 0) { - is_autorepeat = true; - if (!combo.allows_key_repeat()) { - return false; - } - } - result = this._add_to_active_combos(combo, key); - combo.keyup_fired = false; - is_other_exclusive = false; - if (combo.is_exclusive) { - for (_i = 0, _len = potential_combos.length; _i < _len; _i++) { - potential_combo = potential_combos[_i]; - if (potential_combo.is_exclusive && potential_combo.keys.length > combo.keys.length) { - is_other_exclusive = true; - break; - } - } - } - if (!is_other_exclusive) { - if (combo.is_counting && typeof combo.on_keydown === "function") { - combo.count += 1; - } - if (result) { - return this._fire("keydown", combo, e, is_autorepeat); - } - } - }; - - Listener.prototype._key_up = function(key, e) { - var active_combo, active_combos_length, combo, combos, i, sequence_combo, shifted_key, unshifted_key, _i, _j, _k, _l, _len, _len1, _len2, _ref, _ref1, _ref2, _ref3; - unshifted_key = key; - shifted_key = _convert_to_shifted_key(key, e); - if (shifted_key) { - key = shifted_key; - } - shifted_key = _keycode_shifted_keys[unshifted_key]; - if (e.shiftKey) { - if (!(shifted_key && __indexOf.call(this._keys_down, shifted_key) >= 0)) { - key = unshifted_key; - } - } else { - if (!(unshifted_key && __indexOf.call(this._keys_down, unshifted_key) >= 0)) { - key = shifted_key; - } - } - sequence_combo = this._get_sequence(key); - if (sequence_combo) { - this._fire("keyup", sequence_combo, e); - } - if (__indexOf.call(this._keys_down, key) < 0) { - return false; - } - for (i = _i = 0, _ref = this._keys_down.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) { - if ((_ref1 = this._keys_down[i]) === key || _ref1 === shifted_key || _ref1 === unshifted_key) { - this._keys_down.splice(i, 1); - break; - } - } - active_combos_length = this._active_combos.length; - combos = []; - _ref2 = this._active_combos; - for (_j = 0, _len = _ref2.length; _j < _len; _j++) { - active_combo = _ref2[_j]; - if (__indexOf.call(active_combo.keys, key) >= 0) { - combos.push(active_combo); - } - } - for (_k = 0, _len1 = combos.length; _k < _len1; _k++) { - combo = combos[_k]; - this._handle_combo_up(combo, e, key); - } - if (active_combos_length > 1) { - _ref3 = this._active_combos; - for (_l = 0, _len2 = _ref3.length; _l < _len2; _l++) { - active_combo = _ref3[_l]; - if (active_combo === void 0 || __indexOf.call(combos, active_combo) >= 0) { - continue; - } - if (!this._keys_remain(active_combo)) { - this._remove_from_active_combos(active_combo); - } - } - } - }; - - Listener.prototype._handle_combo_up = function(combo, e, key) { - var keys_down, keys_remaining; - this._prevent_default(e, combo && combo.prevent_default); - keys_remaining = this._keys_remain(combo); - if (!combo.keyup_fired) { - keys_down = this._keys_down.slice(); - keys_down.push(key); - if (!combo.is_solitary || _compare_arrays(keys_down, combo.keys)) { - this._fire("keyup", combo, e); - if (combo.is_counting && typeof combo.on_keyup === "function" && typeof combo.on_keydown !== "function") { - combo.count += 1; - } - } - } - if (!keys_remaining) { - this._fire("release", combo, e); - this._remove_from_active_combos(combo); - } - }; - - Listener.prototype.simple_combo = function(keys, callback) { - return this.register_combo({ - keys: keys, - on_keydown: callback - }); - }; - - Listener.prototype.counting_combo = function(keys, count_callback) { - return this.register_combo({ - keys: keys, - is_counting: true, - is_unordered: false, - on_keydown: count_callback - }); - }; - - Listener.prototype.sequence_combo = function(keys, callback) { - return this.register_combo({ - keys: keys, - on_keydown: callback, - is_sequence: true, - is_exclusive: true - }); - }; - - Listener.prototype.register_combo = function(combo_dictionary) { - var combo, property, value, _ref; - if (typeof combo_dictionary["keys"] === "string") { - combo_dictionary["keys"] = combo_dictionary["keys"].split(" "); - } - _ref = this._defaults; - for (property in _ref) { - if (!__hasProp.call(_ref, property)) continue; - value = _ref[property]; - if (combo_dictionary[property] === void 0) { - combo_dictionary[property] = value; - } - } - combo = new Combo(combo_dictionary); - if (_validate_combo(combo)) { - this._registered_combos.push(combo); - return combo; - } - }; - - Listener.prototype.register_many = function(combo_array) { - var combo, _i, _len, _results; - _results = []; - for (_i = 0, _len = combo_array.length; _i < _len; _i++) { - combo = combo_array[_i]; - _results.push(this.register_combo(combo)); - } - return _results; - }; - - Listener.prototype.unregister_combo = function(keys_or_combo) { - var combo, unregister_combo, _i, _len, _ref, _results; - if (!keys_or_combo) { - return false; - } - unregister_combo = (function(_this) { - return function(combo) { - var i, _i, _ref, _results; - _results = []; - for (i = _i = 0, _ref = _this._registered_combos.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) { - if (combo === _this._registered_combos[i]) { - _this._registered_combos.splice(i, 1); - break; - } else { - _results.push(void 0); - } - } - return _results; - }; - })(this); - if (keys_or_combo instanceof Combo) { - return unregister_combo(keys_or_combo); - } else { - if (typeof keys_or_combo === "string") { - keys_or_combo = keys_or_combo.split(" "); - } - _ref = this._registered_combos; - _results = []; - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - combo = _ref[_i]; - if (combo == null) { - continue; - } - if ((combo.is_unordered && _compare_arrays(keys_or_combo, combo.keys)) || (!combo.is_unordered && _compare_arrays_sorted(keys_or_combo, combo.keys))) { - _results.push(unregister_combo(combo)); - } else { - _results.push(void 0); - } - } - return _results; - } - }; - - Listener.prototype.unregister_many = function(combo_array) { - var combo, _i, _len, _results; - _results = []; - for (_i = 0, _len = combo_array.length; _i < _len; _i++) { - combo = combo_array[_i]; - _results.push(this.unregister_combo(combo)); - } - return _results; - }; - - Listener.prototype.get_registered_combos = function() { - return this._registered_combos; - }; - - Listener.prototype.reset = function() { - return this._registered_combos = []; - }; - - Listener.prototype.listen = function() { - return this._prevent_capture = false; - }; - - Listener.prototype.stop_listening = function() { - return this._prevent_capture = true; - }; - - Listener.prototype.get_meta_key = function() { - return _metakey; - }; - - return Listener; - - })(); - - _decide_meta_key = function() { - if (navigator.userAgent.indexOf("Mac OS X") !== -1) { - _metakey = "cmd"; - } - }; - - _change_keycodes_by_browser = function() { - if (navigator.userAgent.indexOf("Opera") !== -1) { - _keycode_dictionary["17"] = "cmd"; - } - }; - - _convert_key_to_readable = function(k) { - return _keycode_dictionary[k]; - }; - - _filter_array = function(array, callback) { - var element; - if (array.filter) { - return array.filter(callback); - } else { - return (function() { - var _i, _len, _results; - _results = []; - for (_i = 0, _len = array.length; _i < _len; _i++) { - element = array[_i]; - if (callback(element)) { - _results.push(element); - } - } - return _results; - })(); - } - }; - - _compare_arrays = function(a1, a2) { - var item, _i, _len; - if (a1.length !== a2.length) { - return false; - } - for (_i = 0, _len = a1.length; _i < _len; _i++) { - item = a1[_i]; - if (__indexOf.call(a2, item) >= 0) { - continue; - } - return false; - } - return true; - }; - - _compare_arrays_sorted = function(a1, a2) { - var i, _i, _ref; - if (a1.length !== a2.length) { - return false; - } - for (i = _i = 0, _ref = a1.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) { - if (a1[i] !== a2[i]) { - return false; - } - } - return true; - }; - - _is_array_in_array = function(a1, a2) { - var item, _i, _len; - for (_i = 0, _len = a1.length; _i < _len; _i++) { - item = a1[_i]; - if (__indexOf.call(a2, item) < 0) { - return false; - } - } - return true; - }; - - _index_of_in_array = Array.prototype.indexOf || function(a, item) { - var i, _i, _ref; - for (i = _i = 0, _ref = a.length; 0 <= _ref ? _i <= _ref : _i >= _ref; i = 0 <= _ref ? ++_i : --_i) { - if (a[i] === item) { - return i; - } - } - return -1; - }; - - _is_array_in_array_sorted = function(a1, a2) { - var index, item, prev, _i, _len; - prev = 0; - for (_i = 0, _len = a1.length; _i < _len; _i++) { - item = a1[_i]; - index = _index_of_in_array.call(a2, item); - if (index >= prev) { - prev = index; - } else { - return false; - } - } - return true; - }; - - _log_error = function() { - if (keypress.debug) { - return console.log.apply(console, arguments); - } - }; - - _key_is_valid = function(key) { - var valid, valid_key, _; - valid = false; - for (_ in _keycode_dictionary) { - valid_key = _keycode_dictionary[_]; - if (key === valid_key) { - valid = true; - break; - } - } - if (!valid) { - for (_ in _keycode_shifted_keys) { - valid_key = _keycode_shifted_keys[_]; - if (key === valid_key) { - valid = true; - break; - } - } - } - return valid; - }; - - _validate_combo = function(combo) { - var alt_name, i, key, mod_key, non_modifier_keys, property, validated, value, _i, _j, _k, _len, _len1, _ref, _ref1; - validated = true; - if (!combo.keys.length) { - _log_error("You're trying to bind a combo with no keys:", combo); - } - for (i = _i = 0, _ref = combo.keys.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) { - key = combo.keys[i]; - alt_name = _keycode_alternate_names[key]; - if (alt_name) { - key = combo.keys[i] = alt_name; - } - if (key === "meta") { - combo.keys.splice(i, 1, _metakey); - } - if (key === "cmd") { - _log_error("Warning: use the \"meta\" key rather than \"cmd\" for Windows compatibility"); - } - } - _ref1 = combo.keys; - for (_j = 0, _len = _ref1.length; _j < _len; _j++) { - key = _ref1[_j]; - if (!_key_is_valid(key)) { - _log_error("Do not recognize the key \"" + key + "\""); - validated = false; - } - } - if (__indexOf.call(combo.keys, "meta") >= 0 || __indexOf.call(combo.keys, "cmd") >= 0) { - non_modifier_keys = combo.keys.slice(); - for (_k = 0, _len1 = _modifier_keys.length; _k < _len1; _k++) { - mod_key = _modifier_keys[_k]; - if ((i = _index_of_in_array.call(non_modifier_keys, mod_key)) > -1) { - non_modifier_keys.splice(i, 1); - } - } - if (non_modifier_keys.length > 1) { - _log_error("META and CMD key combos cannot have more than 1 non-modifier keys", combo, non_modifier_keys); - validated = false; - } - } - for (property in combo) { - value = combo[property]; - if (_factory_defaults[property] === "undefined") { - _log_error("The property " + property + " is not a valid combo property. Your combo has still been registered."); - } - } - return validated; - }; - - _convert_to_shifted_key = function(key, e) { - var k; - if (!e.shiftKey) { - return false; - } - k = _keycode_shifted_keys[key]; - if (k != null) { - return k; - } - return false; - }; - - _modifier_event_mapping = { - "cmd": "metaKey", - "ctrl": "ctrlKey", - "shift": "shiftKey", - "alt": "altKey" - }; - - _keycode_alternate_names = { - "escape": "esc", - "control": "ctrl", - "command": "cmd", - "break": "pause", - "windows": "cmd", - "option": "alt", - "caps_lock": "caps", - "apostrophe": "\'", - "semicolon": ";", - "tilde": "~", - "accent": "`", - "scroll_lock": "scroll", - "num_lock": "num" - }; - - _keycode_shifted_keys = { - "/": "?", - ".": ">", - ",": "<", - "\'": "\"", - ";": ":", - "[": "{", - "]": "}", - "\\": "|", - "`": "~", - "=": "+", - "-": "_", - "1": "!", - "2": "@", - "3": "#", - "4": "$", - "5": "%", - "6": "^", - "7": "&", - "8": "*", - "9": "(", - "0": ")" - }; - - _keycode_dictionary = { - 0: "\\", - 8: "backspace", - 9: "tab", - 12: "num", - 13: "enter", - 16: "shift", - 17: "ctrl", - 18: "alt", - 19: "pause", - 20: "caps", - 27: "esc", - 32: "space", - 33: "pageup", - 34: "pagedown", - 35: "end", - 36: "home", - 37: "left", - 38: "up", - 39: "right", - 40: "down", - 44: "print", - 45: "insert", - 46: "delete", - 48: "0", - 49: "1", - 50: "2", - 51: "3", - 52: "4", - 53: "5", - 54: "6", - 55: "7", - 56: "8", - 57: "9", - 65: "a", - 66: "b", - 67: "c", - 68: "d", - 69: "e", - 70: "f", - 71: "g", - 72: "h", - 73: "i", - 74: "j", - 75: "k", - 76: "l", - 77: "m", - 78: "n", - 79: "o", - 80: "p", - 81: "q", - 82: "r", - 83: "s", - 84: "t", - 85: "u", - 86: "v", - 87: "w", - 88: "x", - 89: "y", - 90: "z", - 91: "cmd", - 92: "cmd", - 93: "cmd", - 96: "num_0", - 97: "num_1", - 98: "num_2", - 99: "num_3", - 100: "num_4", - 101: "num_5", - 102: "num_6", - 103: "num_7", - 104: "num_8", - 105: "num_9", - 106: "num_multiply", - 107: "num_add", - 108: "num_enter", - 109: "num_subtract", - 110: "num_decimal", - 111: "num_divide", - 112: "f1", - 113: "f2", - 114: "f3", - 115: "f4", - 116: "f5", - 117: "f6", - 118: "f7", - 119: "f8", - 120: "f9", - 121: "f10", - 122: "f11", - 123: "f12", - 124: "print", - 144: "num", - 145: "scroll", - 186: ";", - 187: "=", - 188: ",", - 189: "-", - 190: ".", - 191: "/", - 192: "`", - 219: "[", - 220: "\\", - 221: "]", - 222: "\'", - 223: "`", - 224: "cmd", - 225: "alt", - 57392: "ctrl", - 63289: "num", - 59: ";", - 61: "=", - 173: "-" - }; - - keypress._keycode_dictionary = _keycode_dictionary; - - keypress._is_array_in_array_sorted = _is_array_in_array_sorted; - - _decide_meta_key(); - - _change_keycodes_by_browser(); - - if (typeof define === "function" && define.amd) { - define([], function() { - return keypress; - }); - } else if (typeof exports !== "undefined" && exports !== null) { - exports.keypress = keypress; - } else { - window.keypress = keypress; - } - -}).call(this); From 9a7cd5a04c5aa2b1094e94f1e275a813af404ba7 Mon Sep 17 00:00:00 2001 From: Eric Hebert Date: Sun, 5 Mar 2017 19:06:22 -0500 Subject: [PATCH 4/8] Fixed main source path --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 5562ee5..509b815 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "jasmine" : "1.3.0" }, "spm": { - "main": "keypress.js", + "main": "keypress-2.1.4.min.js", "ignore": [ "compiler.jar" ] From c448aa82bee78fe092f6b240127f4c29d2ca0b8f Mon Sep 17 00:00:00 2001 From: Eric Hebert Date: Fri, 22 Dec 2017 14:14:28 -0500 Subject: [PATCH 5/8] Fixed "main" path - not sure how this ever worked before? --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 509b815..e167d71 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name" : "keypress.js", "version" : "2.1.4", - "main" : "keypress.js", + "main" : "keypress-2.1.4.min.js", "description" : "a robust keyboard input capturing Javascript utility focused on input for games. For details and documentation, please visit http://dmauro.github.io/Keypress/", "devDependencies" : { "jasmine" : "1.3.0" From 07c0dba75db87263ec3f771ff59c0b525ab135ce Mon Sep 17 00:00:00 2001 From: Matthew Huebert Date: Thu, 12 Oct 2017 20:00:04 +0200 Subject: [PATCH 6/8] Allow unbind of metakey combos --- keypress-2.1.4.min.js | 56 +- keypress.coffee | 3 + keypress.js | 1155 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 1186 insertions(+), 28 deletions(-) create mode 100644 keypress.js diff --git a/keypress-2.1.4.min.js b/keypress-2.1.4.min.js index 90c4e9e..0a18072 100644 --- a/keypress-2.1.4.min.js +++ b/keypress-2.1.4.min.js @@ -3,31 +3,31 @@ Licensed under the Apache License, Version 2.0 http://www.apache.org/licenses/LICENSE-2.0 */ -(function(){var m,v,y,z,A,r,w,B,F,C,G,H,q,s,p,o,t,D,I,E={}.hasOwnProperty,j=[].indexOf||function(a){for(var c=0,b=this.length;cj.call(a,"cmd")?!1:!0};g.prototype._prevent_default=function(a,c){if((c||this.should_suppress_event_defaults)&&!this.should_force_event_defaults)if(a.preventDefault?a.preventDefault(): -a.returnValue=!1,a.stopPropagation)return a.stopPropagation()};g.prototype._get_active_combos=function(a){var c,b;c=[];b=w(this._keys_down,function(b){return b!==a});b.push(a);this._match_combo_arrays(b,function(a){return function(b){if(a._cmd_bug_check(b.keys))return c.push(b)}}(this));this._fuzzy_match_combo_arrays(b,function(a){return function(b){if(!(0<=j.call(c,b))&&!b.is_solitary&&a._cmd_bug_check(b.keys))return c.push(b)}}(this));return c};g.prototype._get_potential_combos=function(a){var c, -b,d,e,f;b=[];f=this._registered_combos;d=0;for(e=f.length;dk;e=0<=k?++i:--i)if((c=this._active_combos[e])&&c.is_exclusive&&a.is_exclusive){c=c.keys;if(!h){g=0;for(n=c.length;gj.call(a.keys,b)){h=!1;break}}if(f&&!h){l=a.keys;g=0;for(n=l.length;gj.call(c,b)){f=!0;break}}h&&(d?(c=this._active_combos.splice(e,1)[0],null!=c&&c.reset()):(c=this._active_combos.splice(e,1,a)[0],null!=c&&c.reset(),d=!0),f=!1)}}f&&this._active_combos.unshift(a);return h||f};g.prototype._remove_from_active_combos=function(a){var c,b,d,e;b=d=0;for(e=this._active_combos.length;0<=e?de;b=0<=e?++d:--d)if(c=this._active_combos[b],c===a){a=this._active_combos.splice(b, -1)[0];a.reset();break}};g.prototype._get_possible_sequences=function(){var a,c,b,d,e,f,h,i,g,n,k,l;d=[];n=this._registered_combos;f=0;for(g=n.length;f=k;c=1<=k?++h:--h)if(e=this._sequence.slice(-c),a.is_sequence){if(0>j.call(a.keys,"shift")&&(e=w(e,function(a){return"shift"!==a}),!e.length))continue;c=i=0;for(l=e.length;0<=l?il;c=0<=l?++i:--i)if(a.keys[c]===e[c])b=!0;else{b=!1;break}b&&d.push(a)}}return d};g.prototype._add_key_to_sequence= -function(a,c){var b,d,e,f;this._sequence.push(a);d=this._get_possible_sequences();if(d.length){e=0;for(f=d.length;e=l;b=1<=l?++i:--i)if(f=w(this._sequence,function(a){return 0<=j.call(c.keys,"shift")?!0:"shift"!==a}).slice(-b),c.keys.length===f.length){b=g=0;for(u=f.length;0<=u?gu;b=0<=u?++g:--g)if(e=f[b],!(0>j.call(c.keys,"shift")&&"shift"===e)&&!("shift"===a&&0>j.call(c.keys,"shift")))if(c.keys[b]===e)d=!0;else{d=!1;break}}if(d)return c.is_exclusive&&(this._sequence=[]),c}return!1};g.prototype._receive_input=function(a,c){var b,d;if(this._prevent_capture)this._keys_down.length&& -(this._keys_down=[]);else if(b=z(null!=(d=a.keyCode)?d:a.key),(c||this._keys_down.length||!("alt"===b||b===o))&&b)return c?this._key_down(b,a):this._key_up(b,a)};g.prototype._fire=function(a,c,b,d){"function"===typeof c["on_"+a]&&this._prevent_default(b,!0!==c["on_"+a].call(c["this"],b,c.count,d));"release"===a&&(c.count=0);if("keyup"===a)return c.keyup_fired=!0};g.prototype._match_combo_arrays=function(a,c){var b,d,e,f,h;h=this._registered_combos;e=0;for(f=h.length;ef;b=0<=f?++d:--d)this._keys_down[b]===e&&this._keys_down.splice(b,1)}d=this._get_active_combos(a);e=this._get_potential_combos(a);f=0;for(h=d.length;fj.call(this._keys_down,a)&&this._keys_down.push(a)};g.prototype._handle_combo_down=function(a,c,b,d){var e,f,h,g,m;if(0>j.call(a.keys,b))return!1;this._prevent_default(d,a&&a.prevent_default);e=!1;if(0<=j.call(this._keys_down,b)&&(e=!0,!a.allows_key_repeat()))return!1;h=this._add_to_active_combos(a,b);b=a.keyup_fired=!1;if(a.is_exclusive){g=0;for(m=c.length;g -a.keys.length){b=!0;break}}if(!b&&(a.is_counting&&"function"===typeof a.on_keydown&&(a.count+=1),h))return this._fire("keydown",a,d,e)};g.prototype._key_up=function(a,c){var b,d,e,f,h,g;b=a;(e=A(a,c))&&(a=e);e=s[b];c.shiftKey?e&&0<=j.call(this._keys_down,e)||(a=b):b&&0<=j.call(this._keys_down,b)||(a=e);(f=this._get_sequence(a))&&this._fire("keyup",f,c);if(0>j.call(this._keys_down,a))return!1;f=h=0;for(g=this._keys_down.length;0<=g?hg;f=0<=g?++h:--h)if((d=this._keys_down[f])===a||d===e||d===b){this._keys_down.splice(f, -1);break}d=this._active_combos.length;e=[];g=this._active_combos;f=0;for(h=g.length;fd;b=0<=d?++c:--c)if(a===i._registered_combos[b]){i._registered_combos.splice(b,1);break}else e.push(void 0);return e};if(a instanceof x)return b(a);"string"===typeof a&&(a=a.split(" "));f=this._registered_combos;g=[];d=0;for(e=f.length;de;b=0<=e?++d:--d)if(a[b]!==c[b])return!1;return!0};F=function(a,c){var b,d,e;d=0;for(e=a.length;dj.call(c,b))return!1;return!0};B=Array.prototype.indexOf||function(a,c){var b,d,e;b=d=0;for(e=a.length;0<=e?d<=e:d>=e;b=0<=e?++d:--d)if(a[b]=== -c)return b;return-1};C=function(a,c){var b,d,e,f;e=d=0;for(f=a.length;e=d)d=b;else return!1;return!0};p=function(){if(m.debug)return console.log.apply(console,arguments)};G=function(a){var c,b,d;c=!1;for(d in q)if(b=q[d],a===b){c=!0;break}if(!c)for(d in s)if(b=s[d],a===b){c=!0;break}return c};I=function(a){var c,b,d,e,f,g,i;f=!0;a.keys.length||p("You're trying to bind a combo with no keys:",a);b=g=0;for(i=a.keys.length;0<=i?gi;b=0<=i?++g:--g)d=a.keys[b],(c=H[d])&& -(d=a.keys[b]=c),"meta"===d&&a.keys.splice(b,1,o),"cmd"===d&&p('Warning: use the "meta" key rather than "cmd" for Windows compatibility');i=a.keys;c=0;for(g=i.length;c",",":"<","'":'"',";":":","[":"{","]":"}","\\":"|","`":"~","=":"+","-":"_", -1:"!",2:"@",3:"#",4:"$",5:"%",6:"^",7:"&",8:"*",9:"(","0":")"};q={"0":"\\",8:"backspace",9:"tab",12:"num",13:"enter",16:"shift",17:"ctrl",18:"alt",19:"pause",20:"caps",27:"esc",32:"space",33:"pageup",34:"pagedown",35:"end",36:"home",37:"left",38:"up",39:"right",40:"down",44:"print",45:"insert",46:"delete",48:"0",49:"1",50:"2",51:"3",52:"4",53:"5",54:"6",55:"7",56:"8",57:"9",65:"a",66:"b",67:"c",68:"d",69:"e",70:"f",71:"g",72:"h",73:"i",74:"j",75:"k",76:"l",77:"m",78:"n",79:"o",80:"p",81:"q",82:"r", -83:"s",84:"t",85:"u",86:"v",87:"w",88:"x",89:"y",90:"z",91:"cmd",92:"cmd",93:"cmd",96:"num_0",97:"num_1",98:"num_2",99:"num_3",100:"num_4",101:"num_5",102:"num_6",103:"num_7",104:"num_8",105:"num_9",106:"num_multiply",107:"num_add",108:"num_enter",109:"num_subtract",110:"num_decimal",111:"num_divide",112:"f1",113:"f2",114:"f3",115:"f4",116:"f5",117:"f6",118:"f7",119:"f8",120:"f9",121:"f10",122:"f11",123:"f12",124:"print",144:"num",145:"scroll",186:";",187:"=",188:",",189:"-",190:".",191:"/",192:"`", -219:"[",220:"\\",221:"]",222:"'",223:"`",224:"cmd",225:"alt",57392:"ctrl",63289:"num",59:";",61:"=",173:"-"};m._keycode_dictionary=q;m._is_array_in_array_sorted=C;-1!==navigator.userAgent.indexOf("Mac OS X")&&(o="cmd");-1!==navigator.userAgent.indexOf("Opera")&&(q["17"]="cmd");"function"===typeof define&&define.amd?define([],function(){return m}):"undefined"!==typeof exports&&null!==exports?exports.keypress=m:window.keypress=m}).call(this); +(function(){var u,x,y,z,r,v,A,E,B,F,G,q,s,p,l,t,C,H,o,D={}.hasOwnProperty,i=[].indexOf||function(a){for(var c=0,b=this.length;ci.call(a,"cmd")?!1:!0};h.prototype._prevent_default=function(a,c){if((c||this.should_suppress_event_defaults)&&!this.should_force_event_defaults)if(a.preventDefault?a.preventDefault(): +a.returnValue=!1,a.stopPropagation)return a.stopPropagation()};h.prototype._get_active_combos=function(a){var c,b;c=[];b=v(this._keys_down,function(b){return b!==a});b.push(a);this._match_combo_arrays(b,function(a){return function(b){if(a._cmd_bug_check(b.keys))return c.push(b)}}(this));this._fuzzy_match_combo_arrays(b,function(a){return function(b){if(!(0<=i.call(c,b))&&!b.is_solitary&&a._cmd_bug_check(b.keys))return c.push(b)}}(this));return c};h.prototype._get_potential_combos=function(a){var c, +b,d,e,f;e=[];f=this._registered_combos;b=0;for(d=f.length;bh;e=0<=h?++f:--f)if((c=this._active_combos[e])&&c.is_exclusive&&a.is_exclusive){c=c.keys;if(!k){n=0;for(g=c.length;ni.call(a.keys,b)){k=!1;break}}if(j&&!k){m=a.keys;n=0;for(g=m.length;ni.call(c,b)){j=!0;break}}k&&(d?(c=this._active_combos.splice(e,1)[0],null!=c&&c.reset()):(c=this._active_combos.splice(e,1,a)[0],null!=c&&c.reset(),d=!0),j=!1)}}j&&this._active_combos.unshift(a);return k||j};h.prototype._remove_from_active_combos=function(a){var c,b,d,e;b=d=0;for(e=this._active_combos.length;0<=e?de;b=0<=e?++d:--d)if(c=this._active_combos[b],c===a){a=this._active_combos.splice(b, +1)[0];a.reset();break}};h.prototype._get_possible_sequences=function(){var a,c,b,d,e,f,g,n,h,m,j,k;g=[];h=this._registered_combos;b=0;for(d=h.length;b=m;c=1<=m?++e:--e)if(k=this._sequence.slice(-c),a.is_sequence){if(0>i.call(a.keys,"shift")&&(k=v(k,function(a){return"shift"!==a}),!k.length))continue;c=n=0;for(j=k.length;0<=j?nj;c=0<=j?++n:--n)if(a.keys[c]===k[c])f=!0;else{f=!1;break}f&&g.push(a)}}return g};h.prototype._add_key_to_sequence= +function(a,c){var b,d,e,f;this._sequence.push(a);f=this._get_possible_sequences();if(f.length){d=0;for(e=f.length;d=m;b=1<=m?++f:--f)if(o=v(this._sequence,function(a){return 0<=i.call(c.keys,"shift")?!0:"shift"!==a}).slice(-b),c.keys.length===o.length){b=h=0;for(j=o.length;0<=j?hj;b=0<=j?++h:--h)if(k=o[b],!(0>i.call(c.keys,"shift")&&"shift"===k)&&!("shift"===a&&0>i.call(c.keys,"shift")))if(c.keys[b]===k)g=!0;else{g=!1;break}}if(g)return c.is_exclusive&&(this._sequence=[]),c}return!1};h.prototype._receive_input=function(a,c){var b,d;if(this._prevent_capture)this._keys_down.length&& +(this._keys_down=[]);else if(b=y(null!=(d=a.keyCode)?d:a.key),(c||this._keys_down.length||!("alt"===b||b===l))&&b)return c?this._key_down(b,a):this._key_up(b,a)};h.prototype._fire=function(a,c,b,d){"function"===typeof c["on_"+a]&&this._prevent_default(b,!0!==c["on_"+a].call(c["this"],b,c.count,d));"release"===a&&(c.count=0);if("keyup"===a)return c.keyup_fired=!0};h.prototype._match_combo_arrays=function(a,c){var b,d,e,f,g;f=this._registered_combos;d=0;for(e=f.length;de;b=0<=e?++d:--d)this._keys_down[b]===g&&this._keys_down.splice(b,1)}d=this._get_active_combos(a);g=this._get_potential_combos(a);f=0;for(e=d.length;fi.call(this._keys_down,a)&&this._keys_down.push(a)};h.prototype._handle_combo_down=function(a,c,b,d){var e,f,g,h,l;if(0>i.call(a.keys,b))return!1;this._prevent_default(d,a&&a.prevent_default);e=!1;if(0<=i.call(this._keys_down,b)&&(e=!0,!a.allows_key_repeat()))return!1;l=this._add_to_active_combos(a,b);b=a.keyup_fired=!1;if(a.is_exclusive){f=0;for(g=c.length;f +a.keys.length){b=!0;break}}if(!b&&(a.is_counting&&"function"===typeof a.on_keydown&&(a.count+=1),l))return this._fire("keydown",a,d,e)};h.prototype._key_up=function(a,c){var b,d,e,f,g,h;d=a;(b=z(a,c))&&(a=b);b=s[d];c.shiftKey?b&&0<=i.call(this._keys_down,b)||(a=d):d&&0<=i.call(this._keys_down,d)||(a=b);(f=this._get_sequence(a))&&this._fire("keyup",f,c);if(0>i.call(this._keys_down,a))return!1;f=g=0;for(h=this._keys_down.length;0<=h?gh;f=0<=h?++g:--g)if((e=this._keys_down[f])===a||e===b||e===d){this._keys_down.splice(f, +1);break}d=this._active_combos.length;e=[];h=this._active_combos;g=0;for(f=h.length;gd;b=0<=d?++c:--c)if(a===h._registered_combos[b]){h._registered_combos.splice(b,1);break}else e.push(void 0);return e};if(a instanceof w)return g(a);if("string"===typeof a){a=a.split(" ");c=b=0;for(d=a.length;0<=d?bd;c=0<=d?++b:--b)"meta"===a[c]&&(a[c]=l)}e=this._registered_combos;f=[];d=0;for(b=e.length;de;b=0<=e?++d:--d)if(a[b]!==c[b])return!1;return!0};E=function(a,c){var b,d,e;d=0;for(e=a.length;di.call(c,b))return!1;return!0};A=Array.prototype.indexOf||function(a,c){var b,d,e;b=d=0; +for(e=a.length;0<=e?d<=e:d>=e;b=0<=e?++d:--d)if(a[b]===c)return b;return-1};B=function(a,c){var b,d,e,f;d=f=0;for(e=a.length;d=f)f=b;else return!1;return!0};p=function(){if(o.debug)return console.log.apply(console,arguments)};F=function(a){var c,b,d;b=!1;for(c in q)if(d=q[c],a===d){b=!0;break}if(!b)for(c in s)if(d=s[c],a===d){b=!0;break}return b};H=function(a){var c,b,d,e,f,g,h;h=!0;a.keys.length||p("You're trying to bind a combo with no keys:",a);b=e=0;for(f=a.keys.length;0<= +f?ef;b=0<=f?++e:--e)d=a.keys[b],(c=G[d])&&(d=a.keys[b]=c),"meta"===d&&a.keys.splice(b,1,l),"cmd"===d&&p('Warning: use the "meta" key rather than "cmd" for Windows compatibility');f=a.keys;e=0;for(c=f.length;e",",":"<","'":'"', +";":":","[":"{","]":"}","\\":"|","`":"~","=":"+","-":"_",1:"!",2:"@",3:"#",4:"$",5:"%",6:"^",7:"&",8:"*",9:"(","0":")"};q={"0":"\\",8:"backspace",9:"tab",12:"num",13:"enter",16:"shift",17:"ctrl",18:"alt",19:"pause",20:"caps",27:"esc",32:"space",33:"pageup",34:"pagedown",35:"end",36:"home",37:"left",38:"up",39:"right",40:"down",44:"print",45:"insert",46:"delete",48:"0",49:"1",50:"2",51:"3",52:"4",53:"5",54:"6",55:"7",56:"8",57:"9",65:"a",66:"b",67:"c",68:"d",69:"e",70:"f",71:"g",72:"h",73:"i",74:"j", +75:"k",76:"l",77:"m",78:"n",79:"o",80:"p",81:"q",82:"r",83:"s",84:"t",85:"u",86:"v",87:"w",88:"x",89:"y",90:"z",91:"cmd",92:"cmd",93:"cmd",96:"num_0",97:"num_1",98:"num_2",99:"num_3",100:"num_4",101:"num_5",102:"num_6",103:"num_7",104:"num_8",105:"num_9",106:"num_multiply",107:"num_add",108:"num_enter",109:"num_subtract",110:"num_decimal",111:"num_divide",112:"f1",113:"f2",114:"f3",115:"f4",116:"f5",117:"f6",118:"f7",119:"f8",120:"f9",121:"f10",122:"f11",123:"f12",124:"print",144:"num",145:"scroll", +186:";",187:"=",188:",",189:"-",190:".",191:"/",192:"`",219:"[",220:"\\",221:"]",222:"'",223:"`",224:"cmd",225:"alt",57392:"ctrl",63289:"num",59:";",61:"=",173:"-"};o._keycode_dictionary=q;o._is_array_in_array_sorted=B;-1!==navigator.userAgent.indexOf("Mac OS X")&&(l="cmd");-1!==navigator.userAgent.indexOf("Opera")&&(q["17"]="cmd");"function"===typeof define&&define.amd?define([],function(){return o}):"undefined"!==typeof exports&&null!==exports?exports.keypress=o:window.keypress=o}).call(this); diff --git a/keypress.coffee b/keypress.coffee index f4a1891..e525735 100644 --- a/keypress.coffee +++ b/keypress.coffee @@ -585,6 +585,9 @@ class keypress.Listener else if typeof keys_or_combo is "string" keys_or_combo = keys_or_combo.split " " + for i in [0...keys_or_combo.length] + if keys_or_combo[i] == "meta" + keys_or_combo[i] = _metakey for combo in @_registered_combos continue unless combo? if (combo.is_unordered and _compare_arrays(keys_or_combo, combo.keys)) or (not combo.is_unordered and _compare_arrays_sorted(keys_or_combo, combo.keys)) diff --git a/keypress.js b/keypress.js new file mode 100644 index 0000000..cad36ac --- /dev/null +++ b/keypress.js @@ -0,0 +1,1155 @@ +// Generated by CoffeeScript 1.12.7 + +/* +Copyright 2014 David Mauro + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +Keypress is a robust keyboard input capturing Javascript utility +focused on input for games. + +version 2.1.3 + */ + + +/* +Combo options available and their defaults: + keys : [] - An array of the keys pressed together to activate combo. + count : 0 - The number of times a counting combo has been pressed. Reset on release. + is_unordered : false - Unless this is set to true, the keys can be pressed down in any order. + is_counting : false - Makes this a counting combo (see documentation). + is_exclusive : false - This combo will replace other exclusive combos when true. + is_solitary : false - This combo will only fire if ONLY it's keys are pressed down. + is_sequence : false - Rather than a key combo, this is an ordered key sequence. + prevent_default : false - Prevent default behavior for all component key keypresses. + prevent_repeat : false - Prevent the combo from repeating when keydown is held. + normalize_caps_lock : false - Do not allow turning caps lock on to prevent combos from being activated. + on_keydown : null - A function that is called when the combo is pressed. + on_keyup : null - A function that is called when the combo is released. + on_release : null - A function that is called when all keys in the combo are released. + this : undefined - Defines the scope for your callback functions. + */ + +(function() { + var Combo, _change_keycodes_by_browser, _compare_arrays, _compare_arrays_sorted, _convert_key_to_readable, _convert_to_shifted_key, _decide_meta_key, _factory_defaults, _filter_array, _index_of_in_array, _is_array_in_array, _is_array_in_array_sorted, _key_is_valid, _keycode_alternate_names, _keycode_dictionary, _keycode_shifted_keys, _log_error, _metakey, _modifier_event_mapping, _modifier_keys, _validate_combo, keypress, + hasProp = {}.hasOwnProperty, + indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; + + _factory_defaults = { + is_unordered: false, + is_counting: false, + is_exclusive: false, + is_solitary: false, + prevent_default: false, + prevent_repeat: false, + normalize_caps_lock: false + }; + + _modifier_keys = ["meta", "alt", "option", "ctrl", "shift", "cmd"]; + + _metakey = "ctrl"; + + keypress = {}; + + keypress.debug = false; + + Combo = (function() { + function Combo(dictionary) { + var property, value; + for (property in dictionary) { + if (!hasProp.call(dictionary, property)) continue; + value = dictionary[property]; + if (value !== false) { + this[property] = value; + } + } + this.keys = this.keys || []; + this.count = this.count || 0; + } + + Combo.prototype.allows_key_repeat = function() { + return !this.prevent_repeat && typeof this.on_keydown === "function"; + }; + + Combo.prototype.reset = function() { + this.count = 0; + return this.keyup_fired = null; + }; + + return Combo; + + })(); + + keypress.Listener = (function() { + function Listener(element, defaults) { + var attach_handler, property, value; + if ((typeof jQuery !== "undefined" && jQuery !== null) && element instanceof jQuery) { + if (element.length !== 1) { + _log_error("Warning: your jQuery selector should have exactly one object."); + } + element = element[0]; + } + this.should_suppress_event_defaults = false; + this.should_force_event_defaults = false; + this.sequence_delay = 800; + this._registered_combos = []; + this._keys_down = []; + this._active_combos = []; + this._sequence = []; + this._sequence_timer = null; + this._prevent_capture = false; + this._defaults = defaults || {}; + for (property in _factory_defaults) { + if (!hasProp.call(_factory_defaults, property)) continue; + value = _factory_defaults[property]; + this._defaults[property] = this._defaults[property] || value; + } + this.element = element || document.body; + attach_handler = function(target, event, handler) { + if (target.addEventListener) { + target.addEventListener(event, handler); + } else if (target.attachEvent) { + target.attachEvent("on" + event, handler); + } + return handler; + }; + this.keydown_event = attach_handler(this.element, "keydown", (function(_this) { + return function(e) { + e = e || window.event; + _this._receive_input(e, true); + return _this._bug_catcher(e); + }; + })(this)); + this.keyup_event = attach_handler(this.element, "keyup", (function(_this) { + return function(e) { + e = e || window.event; + return _this._receive_input(e, false); + }; + })(this)); + this.blur_event = attach_handler(window, "blur", (function(_this) { + return function() { + var key, l, len, ref; + ref = _this._keys_down; + for (l = 0, len = ref.length; l < len; l++) { + key = ref[l]; + _this._key_up(key, {}); + } + return _this._keys_down = []; + }; + })(this)); + } + + Listener.prototype.destroy = function() { + var remove_handler; + remove_handler = function(target, event, handler) { + if (target.removeEventListener != null) { + return target.removeEventListener(event, handler); + } else if (target.removeEvent != null) { + return target.removeEvent("on" + event, handler); + } + }; + remove_handler(this.element, "keydown", this.keydown_event); + remove_handler(this.element, "keyup", this.keyup_event); + return remove_handler(window, "blur", this.blur_event); + }; + + Listener.prototype._bug_catcher = function(e) { + var ref, ref1; + if (_metakey === "cmd" && indexOf.call(this._keys_down, "cmd") >= 0 && ((ref = _convert_key_to_readable((ref1 = e.keyCode) != null ? ref1 : e.key)) !== "cmd" && ref !== "shift" && ref !== "alt" && ref !== "caps" && ref !== "tab")) { + return this._receive_input(e, false); + } + }; + + Listener.prototype._cmd_bug_check = function(combo_keys) { + if (_metakey === "cmd" && indexOf.call(this._keys_down, "cmd") >= 0 && indexOf.call(combo_keys, "cmd") < 0) { + return false; + } + return true; + }; + + Listener.prototype._prevent_default = function(e, should_prevent) { + if ((should_prevent || this.should_suppress_event_defaults) && !this.should_force_event_defaults) { + if (e.preventDefault) { + e.preventDefault(); + } else { + e.returnValue = false; + } + if (e.stopPropagation) { + return e.stopPropagation(); + } + } + }; + + Listener.prototype._get_active_combos = function(key) { + var active_combos, keys_down; + active_combos = []; + keys_down = _filter_array(this._keys_down, function(down_key) { + return down_key !== key; + }); + keys_down.push(key); + this._match_combo_arrays(keys_down, (function(_this) { + return function(match) { + if (_this._cmd_bug_check(match.keys)) { + return active_combos.push(match); + } + }; + })(this)); + this._fuzzy_match_combo_arrays(keys_down, (function(_this) { + return function(match) { + if (indexOf.call(active_combos, match) >= 0) { + return; + } + if (!(match.is_solitary || !_this._cmd_bug_check(match.keys))) { + return active_combos.push(match); + } + }; + })(this)); + return active_combos; + }; + + Listener.prototype._get_potential_combos = function(key) { + var combo, l, len, potentials, ref; + potentials = []; + ref = this._registered_combos; + for (l = 0, len = ref.length; l < len; l++) { + combo = ref[l]; + if (combo.is_sequence) { + continue; + } + if (indexOf.call(combo.keys, key) >= 0 && this._cmd_bug_check(combo.keys)) { + potentials.push(combo); + } + } + return potentials; + }; + + Listener.prototype._add_to_active_combos = function(combo) { + var active_combo, active_key, active_keys, already_replaced, combo_key, i, l, len, len1, m, n, ref, ref1, should_prepend, should_replace; + should_replace = false; + should_prepend = true; + already_replaced = false; + if (indexOf.call(this._active_combos, combo) >= 0) { + return true; + } else if (this._active_combos.length) { + for (i = l = 0, ref = this._active_combos.length; 0 <= ref ? l < ref : l > ref; i = 0 <= ref ? ++l : --l) { + active_combo = this._active_combos[i]; + if (!(active_combo && active_combo.is_exclusive && combo.is_exclusive)) { + continue; + } + active_keys = active_combo.keys; + if (!should_replace) { + for (m = 0, len = active_keys.length; m < len; m++) { + active_key = active_keys[m]; + should_replace = true; + if (indexOf.call(combo.keys, active_key) < 0) { + should_replace = false; + break; + } + } + } + if (should_prepend && !should_replace) { + ref1 = combo.keys; + for (n = 0, len1 = ref1.length; n < len1; n++) { + combo_key = ref1[n]; + should_prepend = false; + if (indexOf.call(active_keys, combo_key) < 0) { + should_prepend = true; + break; + } + } + } + if (should_replace) { + if (already_replaced) { + active_combo = this._active_combos.splice(i, 1)[0]; + if (active_combo != null) { + active_combo.reset(); + } + } else { + active_combo = this._active_combos.splice(i, 1, combo)[0]; + if (active_combo != null) { + active_combo.reset(); + } + already_replaced = true; + } + should_prepend = false; + } + } + } + if (should_prepend) { + this._active_combos.unshift(combo); + } + return should_replace || should_prepend; + }; + + Listener.prototype._remove_from_active_combos = function(combo) { + var active_combo, i, l, ref; + for (i = l = 0, ref = this._active_combos.length; 0 <= ref ? l < ref : l > ref; i = 0 <= ref ? ++l : --l) { + active_combo = this._active_combos[i]; + if (active_combo === combo) { + combo = this._active_combos.splice(i, 1)[0]; + combo.reset(); + break; + } + } + }; + + Listener.prototype._get_possible_sequences = function() { + var combo, i, j, l, len, m, match, matches, n, ref, ref1, ref2, sequence; + matches = []; + ref = this._registered_combos; + for (l = 0, len = ref.length; l < len; l++) { + combo = ref[l]; + for (j = m = 1, ref1 = this._sequence.length; 1 <= ref1 ? m <= ref1 : m >= ref1; j = 1 <= ref1 ? ++m : --m) { + sequence = this._sequence.slice(-j); + if (!combo.is_sequence) { + continue; + } + if (indexOf.call(combo.keys, "shift") < 0) { + sequence = _filter_array(sequence, function(key) { + return key !== "shift"; + }); + if (!sequence.length) { + continue; + } + } + for (i = n = 0, ref2 = sequence.length; 0 <= ref2 ? n < ref2 : n > ref2; i = 0 <= ref2 ? ++n : --n) { + if (combo.keys[i] === sequence[i]) { + match = true; + } else { + match = false; + break; + } + } + if (match) { + matches.push(combo); + } + } + } + return matches; + }; + + Listener.prototype._add_key_to_sequence = function(key, e) { + var combo, l, len, sequence_combos; + this._sequence.push(key); + sequence_combos = this._get_possible_sequences(); + if (sequence_combos.length) { + for (l = 0, len = sequence_combos.length; l < len; l++) { + combo = sequence_combos[l]; + this._prevent_default(e, combo.prevent_default); + } + if (this._sequence_timer) { + clearTimeout(this._sequence_timer); + } + if (this.sequence_delay > -1) { + this._sequence_timer = setTimeout((function(_this) { + return function() { + return _this._sequence = []; + }; + })(this), this.sequence_delay); + } + } else { + this._sequence = []; + } + }; + + Listener.prototype._get_sequence = function(key) { + var combo, i, j, l, len, m, match, n, ref, ref1, ref2, seq_key, sequence; + ref = this._registered_combos; + for (l = 0, len = ref.length; l < len; l++) { + combo = ref[l]; + if (!combo.is_sequence) { + continue; + } + for (j = m = 1, ref1 = this._sequence.length; 1 <= ref1 ? m <= ref1 : m >= ref1; j = 1 <= ref1 ? ++m : --m) { + sequence = (_filter_array(this._sequence, function(seq_key) { + if (indexOf.call(combo.keys, "shift") >= 0) { + return true; + } + return seq_key !== "shift"; + })).slice(-j); + if (combo.keys.length !== sequence.length) { + continue; + } + for (i = n = 0, ref2 = sequence.length; 0 <= ref2 ? n < ref2 : n > ref2; i = 0 <= ref2 ? ++n : --n) { + seq_key = sequence[i]; + if (indexOf.call(combo.keys, "shift") < 0) { + if (seq_key === "shift") { + continue; + } + } + if (key === "shift" && indexOf.call(combo.keys, "shift") < 0) { + continue; + } + if (combo.keys[i] === seq_key) { + match = true; + } else { + match = false; + break; + } + } + } + if (match) { + if (combo.is_exclusive) { + this._sequence = []; + } + return combo; + } + } + return false; + }; + + Listener.prototype._receive_input = function(e, is_keydown) { + var key, ref; + if (this._prevent_capture) { + if (this._keys_down.length) { + this._keys_down = []; + } + return; + } + key = _convert_key_to_readable((ref = e.keyCode) != null ? ref : e.key); + if (!is_keydown && !this._keys_down.length && (key === "alt" || key === _metakey)) { + return; + } + if (!key) { + return; + } + if (is_keydown) { + return this._key_down(key, e); + } else { + return this._key_up(key, e); + } + }; + + Listener.prototype._fire = function(event, combo, key_event, is_autorepeat) { + if (typeof combo["on_" + event] === "function") { + this._prevent_default(key_event, combo["on_" + event].call(combo["this"], key_event, combo.count, is_autorepeat) !== true); + } + if (event === "release") { + combo.count = 0; + } + if (event === "keyup") { + return combo.keyup_fired = true; + } + }; + + Listener.prototype._match_combo_arrays = function(potential_match, match_handler) { + var combo_potential_match, l, len, ref, source_combo; + ref = this._registered_combos; + for (l = 0, len = ref.length; l < len; l++) { + source_combo = ref[l]; + combo_potential_match = potential_match.slice(0); + if (source_combo.normalize_caps_lock && indexOf.call(combo_potential_match, "caps") >= 0) { + combo_potential_match.splice(combo_potential_match.indexOf("caps"), 1); + } + if ((!source_combo.is_unordered && _compare_arrays_sorted(combo_potential_match, source_combo.keys)) || (source_combo.is_unordered && _compare_arrays(combo_potential_match, source_combo.keys))) { + match_handler(source_combo); + } + } + }; + + Listener.prototype._fuzzy_match_combo_arrays = function(potential_match, match_handler) { + var l, len, ref, source_combo; + ref = this._registered_combos; + for (l = 0, len = ref.length; l < len; l++) { + source_combo = ref[l]; + if ((!source_combo.is_unordered && _is_array_in_array_sorted(source_combo.keys, potential_match)) || (source_combo.is_unordered && _is_array_in_array(source_combo.keys, potential_match))) { + match_handler(source_combo); + } + } + }; + + Listener.prototype._keys_remain = function(combo) { + var key, keys_remain, l, len, ref; + ref = combo.keys; + for (l = 0, len = ref.length; l < len; l++) { + key = ref[l]; + if (indexOf.call(this._keys_down, key) >= 0) { + keys_remain = true; + break; + } + } + return keys_remain; + }; + + Listener.prototype._key_down = function(key, e) { + var combo, combos, event_mod, i, l, len, len1, m, mod, n, potential, potential_combos, ref, sequence_combo, shifted_key; + shifted_key = _convert_to_shifted_key(key, e); + if (shifted_key) { + key = shifted_key; + } + this._add_key_to_sequence(key, e); + sequence_combo = this._get_sequence(key); + if (sequence_combo) { + this._fire("keydown", sequence_combo, e); + } + for (mod in _modifier_event_mapping) { + event_mod = _modifier_event_mapping[mod]; + if (!e[event_mod]) { + continue; + } + if (mod === key || indexOf.call(this._keys_down, mod) >= 0) { + continue; + } + this._keys_down.push(mod); + } + for (mod in _modifier_event_mapping) { + event_mod = _modifier_event_mapping[mod]; + if (mod === key) { + continue; + } + if (indexOf.call(this._keys_down, mod) >= 0 && !e[event_mod]) { + if (mod === "cmd" && _metakey !== "cmd") { + continue; + } + for (i = l = 0, ref = this._keys_down.length; 0 <= ref ? l < ref : l > ref; i = 0 <= ref ? ++l : --l) { + if (this._keys_down[i] === mod) { + this._keys_down.splice(i, 1); + } + } + } + } + combos = this._get_active_combos(key); + potential_combos = this._get_potential_combos(key); + for (m = 0, len = combos.length; m < len; m++) { + combo = combos[m]; + this._handle_combo_down(combo, potential_combos, key, e); + } + if (potential_combos.length) { + for (n = 0, len1 = potential_combos.length; n < len1; n++) { + potential = potential_combos[n]; + this._prevent_default(e, potential.prevent_default); + } + } + if (indexOf.call(this._keys_down, key) < 0) { + this._keys_down.push(key); + } + }; + + Listener.prototype._handle_combo_down = function(combo, potential_combos, key, e) { + var is_autorepeat, is_other_exclusive, l, len, potential_combo, result; + if (indexOf.call(combo.keys, key) < 0) { + return false; + } + this._prevent_default(e, combo && combo.prevent_default); + is_autorepeat = false; + if (indexOf.call(this._keys_down, key) >= 0) { + is_autorepeat = true; + if (!combo.allows_key_repeat()) { + return false; + } + } + result = this._add_to_active_combos(combo, key); + combo.keyup_fired = false; + is_other_exclusive = false; + if (combo.is_exclusive) { + for (l = 0, len = potential_combos.length; l < len; l++) { + potential_combo = potential_combos[l]; + if (potential_combo.is_exclusive && potential_combo.keys.length > combo.keys.length) { + is_other_exclusive = true; + break; + } + } + } + if (!is_other_exclusive) { + if (combo.is_counting && typeof combo.on_keydown === "function") { + combo.count += 1; + } + if (result) { + return this._fire("keydown", combo, e, is_autorepeat); + } + } + }; + + Listener.prototype._key_up = function(key, e) { + var active_combo, active_combos_length, combo, combos, i, l, len, len1, len2, m, n, o, ref, ref1, ref2, ref3, sequence_combo, shifted_key, unshifted_key; + unshifted_key = key; + shifted_key = _convert_to_shifted_key(key, e); + if (shifted_key) { + key = shifted_key; + } + shifted_key = _keycode_shifted_keys[unshifted_key]; + if (e.shiftKey) { + if (!(shifted_key && indexOf.call(this._keys_down, shifted_key) >= 0)) { + key = unshifted_key; + } + } else { + if (!(unshifted_key && indexOf.call(this._keys_down, unshifted_key) >= 0)) { + key = shifted_key; + } + } + sequence_combo = this._get_sequence(key); + if (sequence_combo) { + this._fire("keyup", sequence_combo, e); + } + if (indexOf.call(this._keys_down, key) < 0) { + return false; + } + for (i = l = 0, ref = this._keys_down.length; 0 <= ref ? l < ref : l > ref; i = 0 <= ref ? ++l : --l) { + if ((ref1 = this._keys_down[i]) === key || ref1 === shifted_key || ref1 === unshifted_key) { + this._keys_down.splice(i, 1); + break; + } + } + active_combos_length = this._active_combos.length; + combos = []; + ref2 = this._active_combos; + for (m = 0, len = ref2.length; m < len; m++) { + active_combo = ref2[m]; + if (indexOf.call(active_combo.keys, key) >= 0) { + combos.push(active_combo); + } + } + for (n = 0, len1 = combos.length; n < len1; n++) { + combo = combos[n]; + this._handle_combo_up(combo, e, key); + } + if (active_combos_length > 1) { + ref3 = this._active_combos; + for (o = 0, len2 = ref3.length; o < len2; o++) { + active_combo = ref3[o]; + if (active_combo === void 0 || indexOf.call(combos, active_combo) >= 0) { + continue; + } + if (!this._keys_remain(active_combo)) { + this._remove_from_active_combos(active_combo); + } + } + } + }; + + Listener.prototype._handle_combo_up = function(combo, e, key) { + var keys_down, keys_remaining; + this._prevent_default(e, combo && combo.prevent_default); + keys_remaining = this._keys_remain(combo); + if (!combo.keyup_fired) { + keys_down = this._keys_down.slice(); + keys_down.push(key); + if (!combo.is_solitary || _compare_arrays(keys_down, combo.keys)) { + this._fire("keyup", combo, e); + if (combo.is_counting && typeof combo.on_keyup === "function" && typeof combo.on_keydown !== "function") { + combo.count += 1; + } + } + } + if (!keys_remaining) { + this._fire("release", combo, e); + this._remove_from_active_combos(combo); + } + }; + + Listener.prototype.simple_combo = function(keys, callback) { + return this.register_combo({ + keys: keys, + on_keydown: callback + }); + }; + + Listener.prototype.counting_combo = function(keys, count_callback) { + return this.register_combo({ + keys: keys, + is_counting: true, + is_unordered: false, + on_keydown: count_callback + }); + }; + + Listener.prototype.sequence_combo = function(keys, callback) { + return this.register_combo({ + keys: keys, + on_keydown: callback, + is_sequence: true, + is_exclusive: true + }); + }; + + Listener.prototype.register_combo = function(combo_dictionary) { + var combo, property, ref, value; + if (typeof combo_dictionary["keys"] === "string") { + combo_dictionary["keys"] = combo_dictionary["keys"].split(" "); + } + ref = this._defaults; + for (property in ref) { + if (!hasProp.call(ref, property)) continue; + value = ref[property]; + if (combo_dictionary[property] === void 0) { + combo_dictionary[property] = value; + } + } + combo = new Combo(combo_dictionary); + if (_validate_combo(combo)) { + this._registered_combos.push(combo); + return combo; + } + }; + + Listener.prototype.register_many = function(combo_array) { + var combo, l, len, results; + results = []; + for (l = 0, len = combo_array.length; l < len; l++) { + combo = combo_array[l]; + results.push(this.register_combo(combo)); + } + return results; + }; + + Listener.prototype.unregister_combo = function(keys_or_combo) { + var combo, i, l, len, m, ref, ref1, results, unregister_combo; + if (!keys_or_combo) { + return false; + } + unregister_combo = (function(_this) { + return function(combo) { + var i, l, ref, results; + results = []; + for (i = l = 0, ref = _this._registered_combos.length; 0 <= ref ? l < ref : l > ref; i = 0 <= ref ? ++l : --l) { + if (combo === _this._registered_combos[i]) { + _this._registered_combos.splice(i, 1); + break; + } else { + results.push(void 0); + } + } + return results; + }; + })(this); + if (keys_or_combo instanceof Combo) { + return unregister_combo(keys_or_combo); + } else { + if (typeof keys_or_combo === "string") { + keys_or_combo = keys_or_combo.split(" "); + for (i = l = 0, ref = keys_or_combo.length; 0 <= ref ? l < ref : l > ref; i = 0 <= ref ? ++l : --l) { + if (keys_or_combo[i] === "meta") { + keys_or_combo[i] = _metakey; + } + } + } + ref1 = this._registered_combos; + results = []; + for (m = 0, len = ref1.length; m < len; m++) { + combo = ref1[m]; + if (combo == null) { + continue; + } + if ((combo.is_unordered && _compare_arrays(keys_or_combo, combo.keys)) || (!combo.is_unordered && _compare_arrays_sorted(keys_or_combo, combo.keys))) { + results.push(unregister_combo(combo)); + } else { + results.push(void 0); + } + } + return results; + } + }; + + Listener.prototype.unregister_many = function(combo_array) { + var combo, l, len, results; + results = []; + for (l = 0, len = combo_array.length; l < len; l++) { + combo = combo_array[l]; + results.push(this.unregister_combo(combo)); + } + return results; + }; + + Listener.prototype.get_registered_combos = function() { + return this._registered_combos; + }; + + Listener.prototype.reset = function() { + return this._registered_combos = []; + }; + + Listener.prototype.listen = function() { + return this._prevent_capture = false; + }; + + Listener.prototype.stop_listening = function() { + return this._prevent_capture = true; + }; + + Listener.prototype.get_meta_key = function() { + return _metakey; + }; + + return Listener; + + })(); + + _decide_meta_key = function() { + if (navigator.userAgent.indexOf("Mac OS X") !== -1) { + _metakey = "cmd"; + } + }; + + _change_keycodes_by_browser = function() { + if (navigator.userAgent.indexOf("Opera") !== -1) { + _keycode_dictionary["17"] = "cmd"; + } + }; + + _convert_key_to_readable = function(k) { + return _keycode_dictionary[k]; + }; + + _filter_array = function(array, callback) { + var element; + if (array.filter) { + return array.filter(callback); + } else { + return (function() { + var l, len, results; + results = []; + for (l = 0, len = array.length; l < len; l++) { + element = array[l]; + if (callback(element)) { + results.push(element); + } + } + return results; + })(); + } + }; + + _compare_arrays = function(a1, a2) { + var item, l, len; + if (a1.length !== a2.length) { + return false; + } + for (l = 0, len = a1.length; l < len; l++) { + item = a1[l]; + if (indexOf.call(a2, item) >= 0) { + continue; + } + return false; + } + return true; + }; + + _compare_arrays_sorted = function(a1, a2) { + var i, l, ref; + if (a1.length !== a2.length) { + return false; + } + for (i = l = 0, ref = a1.length; 0 <= ref ? l < ref : l > ref; i = 0 <= ref ? ++l : --l) { + if (a1[i] !== a2[i]) { + return false; + } + } + return true; + }; + + _is_array_in_array = function(a1, a2) { + var item, l, len; + for (l = 0, len = a1.length; l < len; l++) { + item = a1[l]; + if (indexOf.call(a2, item) < 0) { + return false; + } + } + return true; + }; + + _index_of_in_array = Array.prototype.indexOf || function(a, item) { + var i, l, ref; + for (i = l = 0, ref = a.length; 0 <= ref ? l <= ref : l >= ref; i = 0 <= ref ? ++l : --l) { + if (a[i] === item) { + return i; + } + } + return -1; + }; + + _is_array_in_array_sorted = function(a1, a2) { + var index, item, l, len, prev; + prev = 0; + for (l = 0, len = a1.length; l < len; l++) { + item = a1[l]; + index = _index_of_in_array.call(a2, item); + if (index >= prev) { + prev = index; + } else { + return false; + } + } + return true; + }; + + _log_error = function() { + if (keypress.debug) { + return console.log.apply(console, arguments); + } + }; + + _key_is_valid = function(key) { + var _, valid, valid_key; + valid = false; + for (_ in _keycode_dictionary) { + valid_key = _keycode_dictionary[_]; + if (key === valid_key) { + valid = true; + break; + } + } + if (!valid) { + for (_ in _keycode_shifted_keys) { + valid_key = _keycode_shifted_keys[_]; + if (key === valid_key) { + valid = true; + break; + } + } + } + return valid; + }; + + _validate_combo = function(combo) { + var alt_name, i, key, l, len, len1, m, mod_key, n, non_modifier_keys, property, ref, ref1, validated, value; + validated = true; + if (!combo.keys.length) { + _log_error("You're trying to bind a combo with no keys:", combo); + } + for (i = l = 0, ref = combo.keys.length; 0 <= ref ? l < ref : l > ref; i = 0 <= ref ? ++l : --l) { + key = combo.keys[i]; + alt_name = _keycode_alternate_names[key]; + if (alt_name) { + key = combo.keys[i] = alt_name; + } + if (key === "meta") { + combo.keys.splice(i, 1, _metakey); + } + if (key === "cmd") { + _log_error("Warning: use the \"meta\" key rather than \"cmd\" for Windows compatibility"); + } + } + ref1 = combo.keys; + for (m = 0, len = ref1.length; m < len; m++) { + key = ref1[m]; + if (!_key_is_valid(key)) { + _log_error("Do not recognize the key \"" + key + "\""); + validated = false; + } + } + if (indexOf.call(combo.keys, "meta") >= 0 || indexOf.call(combo.keys, "cmd") >= 0) { + non_modifier_keys = combo.keys.slice(); + for (n = 0, len1 = _modifier_keys.length; n < len1; n++) { + mod_key = _modifier_keys[n]; + if ((i = _index_of_in_array.call(non_modifier_keys, mod_key)) > -1) { + non_modifier_keys.splice(i, 1); + } + } + if (non_modifier_keys.length > 1) { + _log_error("META and CMD key combos cannot have more than 1 non-modifier keys", combo, non_modifier_keys); + validated = false; + } + } + for (property in combo) { + value = combo[property]; + if (_factory_defaults[property] === "undefined") { + _log_error("The property " + property + " is not a valid combo property. Your combo has still been registered."); + } + } + return validated; + }; + + _convert_to_shifted_key = function(key, e) { + var k; + if (!e.shiftKey) { + return false; + } + k = _keycode_shifted_keys[key]; + if (k != null) { + return k; + } + return false; + }; + + _modifier_event_mapping = { + "cmd": "metaKey", + "ctrl": "ctrlKey", + "shift": "shiftKey", + "alt": "altKey" + }; + + _keycode_alternate_names = { + "escape": "esc", + "control": "ctrl", + "command": "cmd", + "break": "pause", + "windows": "cmd", + "option": "alt", + "caps_lock": "caps", + "apostrophe": "\'", + "semicolon": ";", + "tilde": "~", + "accent": "`", + "scroll_lock": "scroll", + "num_lock": "num" + }; + + _keycode_shifted_keys = { + "/": "?", + ".": ">", + ",": "<", + "\'": "\"", + ";": ":", + "[": "{", + "]": "}", + "\\": "|", + "`": "~", + "=": "+", + "-": "_", + "1": "!", + "2": "@", + "3": "#", + "4": "$", + "5": "%", + "6": "^", + "7": "&", + "8": "*", + "9": "(", + "0": ")" + }; + + _keycode_dictionary = { + 0: "\\", + 8: "backspace", + 9: "tab", + 12: "num", + 13: "enter", + 16: "shift", + 17: "ctrl", + 18: "alt", + 19: "pause", + 20: "caps", + 27: "esc", + 32: "space", + 33: "pageup", + 34: "pagedown", + 35: "end", + 36: "home", + 37: "left", + 38: "up", + 39: "right", + 40: "down", + 44: "print", + 45: "insert", + 46: "delete", + 48: "0", + 49: "1", + 50: "2", + 51: "3", + 52: "4", + 53: "5", + 54: "6", + 55: "7", + 56: "8", + 57: "9", + 65: "a", + 66: "b", + 67: "c", + 68: "d", + 69: "e", + 70: "f", + 71: "g", + 72: "h", + 73: "i", + 74: "j", + 75: "k", + 76: "l", + 77: "m", + 78: "n", + 79: "o", + 80: "p", + 81: "q", + 82: "r", + 83: "s", + 84: "t", + 85: "u", + 86: "v", + 87: "w", + 88: "x", + 89: "y", + 90: "z", + 91: "cmd", + 92: "cmd", + 93: "cmd", + 96: "num_0", + 97: "num_1", + 98: "num_2", + 99: "num_3", + 100: "num_4", + 101: "num_5", + 102: "num_6", + 103: "num_7", + 104: "num_8", + 105: "num_9", + 106: "num_multiply", + 107: "num_add", + 108: "num_enter", + 109: "num_subtract", + 110: "num_decimal", + 111: "num_divide", + 112: "f1", + 113: "f2", + 114: "f3", + 115: "f4", + 116: "f5", + 117: "f6", + 118: "f7", + 119: "f8", + 120: "f9", + 121: "f10", + 122: "f11", + 123: "f12", + 124: "print", + 144: "num", + 145: "scroll", + 186: ";", + 187: "=", + 188: ",", + 189: "-", + 190: ".", + 191: "/", + 192: "`", + 219: "[", + 220: "\\", + 221: "]", + 222: "\'", + 223: "`", + 224: "cmd", + 225: "alt", + 57392: "ctrl", + 63289: "num", + 59: ";", + 61: "=", + 173: "-" + }; + + keypress._keycode_dictionary = _keycode_dictionary; + + keypress._is_array_in_array_sorted = _is_array_in_array_sorted; + + _decide_meta_key(); + + _change_keycodes_by_browser(); + + if (typeof define === "function" && define.amd) { + define([], function() { + return keypress; + }); + } else if (typeof exports !== "undefined" && exports !== null) { + exports.keypress = keypress; + } else { + window.keypress = keypress; + } + +}).call(this); From 969f9d0ab7ffc7987c772cb4b7a8365bf2bc9c12 Mon Sep 17 00:00:00 2001 From: David Mauro Date: Mon, 5 Mar 2018 08:47:07 -0500 Subject: [PATCH 7/8] Testing unregistering meta combo from string input --- keypress.js | 1939 ++++++++++++++++++++++----------------------- test/tests.coffee | 10 + 2 files changed, 977 insertions(+), 972 deletions(-) diff --git a/keypress.js b/keypress.js index cad36ac..21b70c4 100644 --- a/keypress.js +++ b/keypress.js @@ -1,4 +1,4 @@ -// Generated by CoffeeScript 1.12.7 +// Generated by CoffeeScript 1.8.0 /* Copyright 2014 David Mauro @@ -21,7 +21,6 @@ focused on input for games. version 2.1.3 */ - /* Combo options available and their defaults: keys : [] - An array of the keys pressed together to activate combo. @@ -39,1117 +38,1113 @@ Combo options available and their defaults: on_release : null - A function that is called when all keys in the combo are released. this : undefined - Defines the scope for your callback functions. */ +var Combo, keypress, _change_keycodes_by_browser, _compare_arrays, _compare_arrays_sorted, _convert_key_to_readable, _convert_to_shifted_key, _decide_meta_key, _factory_defaults, _filter_array, _index_of_in_array, _is_array_in_array, _is_array_in_array_sorted, _key_is_valid, _keycode_alternate_names, _keycode_dictionary, _keycode_shifted_keys, _log_error, _metakey, _modifier_event_mapping, _modifier_keys, _validate_combo, + __hasProp = {}.hasOwnProperty, + __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; -(function() { - var Combo, _change_keycodes_by_browser, _compare_arrays, _compare_arrays_sorted, _convert_key_to_readable, _convert_to_shifted_key, _decide_meta_key, _factory_defaults, _filter_array, _index_of_in_array, _is_array_in_array, _is_array_in_array_sorted, _key_is_valid, _keycode_alternate_names, _keycode_dictionary, _keycode_shifted_keys, _log_error, _metakey, _modifier_event_mapping, _modifier_keys, _validate_combo, keypress, - hasProp = {}.hasOwnProperty, - indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; - - _factory_defaults = { - is_unordered: false, - is_counting: false, - is_exclusive: false, - is_solitary: false, - prevent_default: false, - prevent_repeat: false, - normalize_caps_lock: false - }; +_factory_defaults = { + is_unordered: false, + is_counting: false, + is_exclusive: false, + is_solitary: false, + prevent_default: false, + prevent_repeat: false, + normalize_caps_lock: false +}; - _modifier_keys = ["meta", "alt", "option", "ctrl", "shift", "cmd"]; +_modifier_keys = ["meta", "alt", "option", "ctrl", "shift", "cmd"]; - _metakey = "ctrl"; +_metakey = "ctrl"; - keypress = {}; +keypress = {}; - keypress.debug = false; +keypress.debug = false; - Combo = (function() { - function Combo(dictionary) { - var property, value; - for (property in dictionary) { - if (!hasProp.call(dictionary, property)) continue; - value = dictionary[property]; - if (value !== false) { - this[property] = value; - } +Combo = (function() { + function Combo(dictionary) { + var property, value; + for (property in dictionary) { + if (!__hasProp.call(dictionary, property)) continue; + value = dictionary[property]; + if (value !== false) { + this[property] = value; } - this.keys = this.keys || []; - this.count = this.count || 0; } + this.keys = this.keys || []; + this.count = this.count || 0; + } - Combo.prototype.allows_key_repeat = function() { - return !this.prevent_repeat && typeof this.on_keydown === "function"; - }; + Combo.prototype.allows_key_repeat = function() { + return !this.prevent_repeat && typeof this.on_keydown === "function"; + }; - Combo.prototype.reset = function() { - this.count = 0; - return this.keyup_fired = null; - }; + Combo.prototype.reset = function() { + this.count = 0; + return this.keyup_fired = null; + }; - return Combo; + return Combo; - })(); +})(); - keypress.Listener = (function() { - function Listener(element, defaults) { - var attach_handler, property, value; - if ((typeof jQuery !== "undefined" && jQuery !== null) && element instanceof jQuery) { - if (element.length !== 1) { - _log_error("Warning: your jQuery selector should have exactly one object."); - } - element = element[0]; - } - this.should_suppress_event_defaults = false; - this.should_force_event_defaults = false; - this.sequence_delay = 800; - this._registered_combos = []; - this._keys_down = []; - this._active_combos = []; - this._sequence = []; - this._sequence_timer = null; - this._prevent_capture = false; - this._defaults = defaults || {}; - for (property in _factory_defaults) { - if (!hasProp.call(_factory_defaults, property)) continue; - value = _factory_defaults[property]; - this._defaults[property] = this._defaults[property] || value; +keypress.Listener = (function() { + function Listener(element, defaults) { + var attach_handler, property, value; + if ((typeof jQuery !== "undefined" && jQuery !== null) && element instanceof jQuery) { + if (element.length !== 1) { + _log_error("Warning: your jQuery selector should have exactly one object."); } - this.element = element || document.body; - attach_handler = function(target, event, handler) { - if (target.addEventListener) { - target.addEventListener(event, handler); - } else if (target.attachEvent) { - target.attachEvent("on" + event, handler); - } - return handler; + element = element[0]; + } + this.should_suppress_event_defaults = false; + this.should_force_event_defaults = false; + this.sequence_delay = 800; + this._registered_combos = []; + this._keys_down = []; + this._active_combos = []; + this._sequence = []; + this._sequence_timer = null; + this._prevent_capture = false; + this._defaults = defaults || {}; + for (property in _factory_defaults) { + if (!__hasProp.call(_factory_defaults, property)) continue; + value = _factory_defaults[property]; + this._defaults[property] = this._defaults[property] || value; + } + this.element = element || document.body; + attach_handler = function(target, event, handler) { + if (target.addEventListener) { + target.addEventListener(event, handler); + } else if (target.attachEvent) { + target.attachEvent("on" + event, handler); + } + return handler; + }; + this.keydown_event = attach_handler(this.element, "keydown", (function(_this) { + return function(e) { + e = e || window.event; + _this._receive_input(e, true); + return _this._bug_catcher(e); }; - this.keydown_event = attach_handler(this.element, "keydown", (function(_this) { - return function(e) { - e = e || window.event; - _this._receive_input(e, true); - return _this._bug_catcher(e); - }; - })(this)); - this.keyup_event = attach_handler(this.element, "keyup", (function(_this) { - return function(e) { - e = e || window.event; - return _this._receive_input(e, false); - }; - })(this)); - this.blur_event = attach_handler(window, "blur", (function(_this) { - return function() { - var key, l, len, ref; - ref = _this._keys_down; - for (l = 0, len = ref.length; l < len; l++) { - key = ref[l]; - _this._key_up(key, {}); - } - return _this._keys_down = []; - }; - })(this)); - } - - Listener.prototype.destroy = function() { - var remove_handler; - remove_handler = function(target, event, handler) { - if (target.removeEventListener != null) { - return target.removeEventListener(event, handler); - } else if (target.removeEvent != null) { - return target.removeEvent("on" + event, handler); + })(this)); + this.keyup_event = attach_handler(this.element, "keyup", (function(_this) { + return function(e) { + e = e || window.event; + return _this._receive_input(e, false); + }; + })(this)); + this.blur_event = attach_handler(window, "blur", (function(_this) { + return function() { + var key, _i, _len, _ref; + _ref = _this._keys_down; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + key = _ref[_i]; + _this._key_up(key, {}); } + return _this._keys_down = []; }; - remove_handler(this.element, "keydown", this.keydown_event); - remove_handler(this.element, "keyup", this.keyup_event); - return remove_handler(window, "blur", this.blur_event); - }; + })(this)); + } - Listener.prototype._bug_catcher = function(e) { - var ref, ref1; - if (_metakey === "cmd" && indexOf.call(this._keys_down, "cmd") >= 0 && ((ref = _convert_key_to_readable((ref1 = e.keyCode) != null ? ref1 : e.key)) !== "cmd" && ref !== "shift" && ref !== "alt" && ref !== "caps" && ref !== "tab")) { - return this._receive_input(e, false); + Listener.prototype.destroy = function() { + var remove_handler; + remove_handler = function(target, event, handler) { + if (target.removeEventListener != null) { + return target.removeEventListener(event, handler); + } else if (target.removeEvent != null) { + return target.removeEvent("on" + event, handler); } }; + remove_handler(this.element, "keydown", this.keydown_event); + remove_handler(this.element, "keyup", this.keyup_event); + return remove_handler(window, "blur", this.blur_event); + }; - Listener.prototype._cmd_bug_check = function(combo_keys) { - if (_metakey === "cmd" && indexOf.call(this._keys_down, "cmd") >= 0 && indexOf.call(combo_keys, "cmd") < 0) { - return false; - } - return true; - }; + Listener.prototype._bug_catcher = function(e) { + var _ref, _ref1; + if (_metakey === "cmd" && __indexOf.call(this._keys_down, "cmd") >= 0 && ((_ref = _convert_key_to_readable((_ref1 = e.keyCode) != null ? _ref1 : e.key)) !== "cmd" && _ref !== "shift" && _ref !== "alt" && _ref !== "caps" && _ref !== "tab")) { + return this._receive_input(e, false); + } + }; - Listener.prototype._prevent_default = function(e, should_prevent) { - if ((should_prevent || this.should_suppress_event_defaults) && !this.should_force_event_defaults) { - if (e.preventDefault) { - e.preventDefault(); - } else { - e.returnValue = false; - } - if (e.stopPropagation) { - return e.stopPropagation(); - } - } - }; + Listener.prototype._cmd_bug_check = function(combo_keys) { + if (_metakey === "cmd" && __indexOf.call(this._keys_down, "cmd") >= 0 && __indexOf.call(combo_keys, "cmd") < 0) { + return false; + } + return true; + }; - Listener.prototype._get_active_combos = function(key) { - var active_combos, keys_down; - active_combos = []; - keys_down = _filter_array(this._keys_down, function(down_key) { - return down_key !== key; - }); - keys_down.push(key); - this._match_combo_arrays(keys_down, (function(_this) { - return function(match) { - if (_this._cmd_bug_check(match.keys)) { - return active_combos.push(match); - } - }; - })(this)); - this._fuzzy_match_combo_arrays(keys_down, (function(_this) { - return function(match) { - if (indexOf.call(active_combos, match) >= 0) { - return; - } - if (!(match.is_solitary || !_this._cmd_bug_check(match.keys))) { - return active_combos.push(match); - } - }; - })(this)); - return active_combos; - }; + Listener.prototype._prevent_default = function(e, should_prevent) { + if ((should_prevent || this.should_suppress_event_defaults) && !this.should_force_event_defaults) { + if (e.preventDefault) { + e.preventDefault(); + } else { + e.returnValue = false; + } + if (e.stopPropagation) { + return e.stopPropagation(); + } + } + }; - Listener.prototype._get_potential_combos = function(key) { - var combo, l, len, potentials, ref; - potentials = []; - ref = this._registered_combos; - for (l = 0, len = ref.length; l < len; l++) { - combo = ref[l]; - if (combo.is_sequence) { - continue; + Listener.prototype._get_active_combos = function(key) { + var active_combos, keys_down; + active_combos = []; + keys_down = _filter_array(this._keys_down, function(down_key) { + return down_key !== key; + }); + keys_down.push(key); + this._match_combo_arrays(keys_down, (function(_this) { + return function(match) { + if (_this._cmd_bug_check(match.keys)) { + return active_combos.push(match); } - if (indexOf.call(combo.keys, key) >= 0 && this._cmd_bug_check(combo.keys)) { - potentials.push(combo); + }; + })(this)); + this._fuzzy_match_combo_arrays(keys_down, (function(_this) { + return function(match) { + if (__indexOf.call(active_combos, match) >= 0) { + return; } - } - return potentials; - }; - - Listener.prototype._add_to_active_combos = function(combo) { - var active_combo, active_key, active_keys, already_replaced, combo_key, i, l, len, len1, m, n, ref, ref1, should_prepend, should_replace; - should_replace = false; - should_prepend = true; - already_replaced = false; - if (indexOf.call(this._active_combos, combo) >= 0) { - return true; - } else if (this._active_combos.length) { - for (i = l = 0, ref = this._active_combos.length; 0 <= ref ? l < ref : l > ref; i = 0 <= ref ? ++l : --l) { - active_combo = this._active_combos[i]; - if (!(active_combo && active_combo.is_exclusive && combo.is_exclusive)) { - continue; - } - active_keys = active_combo.keys; - if (!should_replace) { - for (m = 0, len = active_keys.length; m < len; m++) { - active_key = active_keys[m]; - should_replace = true; - if (indexOf.call(combo.keys, active_key) < 0) { - should_replace = false; - break; - } - } - } - if (should_prepend && !should_replace) { - ref1 = combo.keys; - for (n = 0, len1 = ref1.length; n < len1; n++) { - combo_key = ref1[n]; - should_prepend = false; - if (indexOf.call(active_keys, combo_key) < 0) { - should_prepend = true; - break; - } - } - } - if (should_replace) { - if (already_replaced) { - active_combo = this._active_combos.splice(i, 1)[0]; - if (active_combo != null) { - active_combo.reset(); - } - } else { - active_combo = this._active_combos.splice(i, 1, combo)[0]; - if (active_combo != null) { - active_combo.reset(); - } - already_replaced = true; - } - should_prepend = false; - } + if (!(match.is_solitary || !_this._cmd_bug_check(match.keys))) { + return active_combos.push(match); } + }; + })(this)); + return active_combos; + }; + + Listener.prototype._get_potential_combos = function(key) { + var combo, potentials, _i, _len, _ref; + potentials = []; + _ref = this._registered_combos; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + combo = _ref[_i]; + if (combo.is_sequence) { + continue; } - if (should_prepend) { - this._active_combos.unshift(combo); + if (__indexOf.call(combo.keys, key) >= 0 && this._cmd_bug_check(combo.keys)) { + potentials.push(combo); } - return should_replace || should_prepend; - }; + } + return potentials; + }; - Listener.prototype._remove_from_active_combos = function(combo) { - var active_combo, i, l, ref; - for (i = l = 0, ref = this._active_combos.length; 0 <= ref ? l < ref : l > ref; i = 0 <= ref ? ++l : --l) { + Listener.prototype._add_to_active_combos = function(combo) { + var active_combo, active_key, active_keys, already_replaced, combo_key, i, should_prepend, should_replace, _i, _j, _k, _len, _len1, _ref, _ref1; + should_replace = false; + should_prepend = true; + already_replaced = false; + if (__indexOf.call(this._active_combos, combo) >= 0) { + return true; + } else if (this._active_combos.length) { + for (i = _i = 0, _ref = this._active_combos.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) { active_combo = this._active_combos[i]; - if (active_combo === combo) { - combo = this._active_combos.splice(i, 1)[0]; - combo.reset(); - break; + if (!(active_combo && active_combo.is_exclusive && combo.is_exclusive)) { + continue; } - } - }; - - Listener.prototype._get_possible_sequences = function() { - var combo, i, j, l, len, m, match, matches, n, ref, ref1, ref2, sequence; - matches = []; - ref = this._registered_combos; - for (l = 0, len = ref.length; l < len; l++) { - combo = ref[l]; - for (j = m = 1, ref1 = this._sequence.length; 1 <= ref1 ? m <= ref1 : m >= ref1; j = 1 <= ref1 ? ++m : --m) { - sequence = this._sequence.slice(-j); - if (!combo.is_sequence) { - continue; - } - if (indexOf.call(combo.keys, "shift") < 0) { - sequence = _filter_array(sequence, function(key) { - return key !== "shift"; - }); - if (!sequence.length) { - continue; + active_keys = active_combo.keys; + if (!should_replace) { + for (_j = 0, _len = active_keys.length; _j < _len; _j++) { + active_key = active_keys[_j]; + should_replace = true; + if (__indexOf.call(combo.keys, active_key) < 0) { + should_replace = false; + break; } } - for (i = n = 0, ref2 = sequence.length; 0 <= ref2 ? n < ref2 : n > ref2; i = 0 <= ref2 ? ++n : --n) { - if (combo.keys[i] === sequence[i]) { - match = true; - } else { - match = false; + } + if (should_prepend && !should_replace) { + _ref1 = combo.keys; + for (_k = 0, _len1 = _ref1.length; _k < _len1; _k++) { + combo_key = _ref1[_k]; + should_prepend = false; + if (__indexOf.call(active_keys, combo_key) < 0) { + should_prepend = true; break; } } - if (match) { - matches.push(combo); + } + if (should_replace) { + if (already_replaced) { + active_combo = this._active_combos.splice(i, 1)[0]; + if (active_combo != null) { + active_combo.reset(); + } + } else { + active_combo = this._active_combos.splice(i, 1, combo)[0]; + if (active_combo != null) { + active_combo.reset(); + } + already_replaced = true; } + should_prepend = false; } } - return matches; - }; + } + if (should_prepend) { + this._active_combos.unshift(combo); + } + return should_replace || should_prepend; + }; - Listener.prototype._add_key_to_sequence = function(key, e) { - var combo, l, len, sequence_combos; - this._sequence.push(key); - sequence_combos = this._get_possible_sequences(); - if (sequence_combos.length) { - for (l = 0, len = sequence_combos.length; l < len; l++) { - combo = sequence_combos[l]; - this._prevent_default(e, combo.prevent_default); - } - if (this._sequence_timer) { - clearTimeout(this._sequence_timer); - } - if (this.sequence_delay > -1) { - this._sequence_timer = setTimeout((function(_this) { - return function() { - return _this._sequence = []; - }; - })(this), this.sequence_delay); - } - } else { - this._sequence = []; + Listener.prototype._remove_from_active_combos = function(combo) { + var active_combo, i, _i, _ref; + for (i = _i = 0, _ref = this._active_combos.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) { + active_combo = this._active_combos[i]; + if (active_combo === combo) { + combo = this._active_combos.splice(i, 1)[0]; + combo.reset(); + break; } - }; + } + }; - Listener.prototype._get_sequence = function(key) { - var combo, i, j, l, len, m, match, n, ref, ref1, ref2, seq_key, sequence; - ref = this._registered_combos; - for (l = 0, len = ref.length; l < len; l++) { - combo = ref[l]; + Listener.prototype._get_possible_sequences = function() { + var combo, i, j, match, matches, sequence, _i, _j, _k, _len, _ref, _ref1, _ref2; + matches = []; + _ref = this._registered_combos; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + combo = _ref[_i]; + for (j = _j = 1, _ref1 = this._sequence.length; 1 <= _ref1 ? _j <= _ref1 : _j >= _ref1; j = 1 <= _ref1 ? ++_j : --_j) { + sequence = this._sequence.slice(-j); if (!combo.is_sequence) { continue; } - for (j = m = 1, ref1 = this._sequence.length; 1 <= ref1 ? m <= ref1 : m >= ref1; j = 1 <= ref1 ? ++m : --m) { - sequence = (_filter_array(this._sequence, function(seq_key) { - if (indexOf.call(combo.keys, "shift") >= 0) { - return true; - } - return seq_key !== "shift"; - })).slice(-j); - if (combo.keys.length !== sequence.length) { + if (__indexOf.call(combo.keys, "shift") < 0) { + sequence = _filter_array(sequence, function(key) { + return key !== "shift"; + }); + if (!sequence.length) { continue; } - for (i = n = 0, ref2 = sequence.length; 0 <= ref2 ? n < ref2 : n > ref2; i = 0 <= ref2 ? ++n : --n) { - seq_key = sequence[i]; - if (indexOf.call(combo.keys, "shift") < 0) { - if (seq_key === "shift") { - continue; - } - } - if (key === "shift" && indexOf.call(combo.keys, "shift") < 0) { - continue; - } - if (combo.keys[i] === seq_key) { - match = true; - } else { - match = false; - break; - } + } + for (i = _k = 0, _ref2 = sequence.length; 0 <= _ref2 ? _k < _ref2 : _k > _ref2; i = 0 <= _ref2 ? ++_k : --_k) { + if (combo.keys[i] === sequence[i]) { + match = true; + } else { + match = false; + break; } } if (match) { - if (combo.is_exclusive) { - this._sequence = []; - } - return combo; + matches.push(combo); } } - return false; - }; + } + return matches; + }; - Listener.prototype._receive_input = function(e, is_keydown) { - var key, ref; - if (this._prevent_capture) { - if (this._keys_down.length) { - this._keys_down = []; - } - return; + Listener.prototype._add_key_to_sequence = function(key, e) { + var combo, sequence_combos, _i, _len; + this._sequence.push(key); + sequence_combos = this._get_possible_sequences(); + if (sequence_combos.length) { + for (_i = 0, _len = sequence_combos.length; _i < _len; _i++) { + combo = sequence_combos[_i]; + this._prevent_default(e, combo.prevent_default); + } + if (this._sequence_timer) { + clearTimeout(this._sequence_timer); + } + if (this.sequence_delay > -1) { + this._sequence_timer = setTimeout((function(_this) { + return function() { + return _this._sequence = []; + }; + })(this), this.sequence_delay); } - key = _convert_key_to_readable((ref = e.keyCode) != null ? ref : e.key); - if (!is_keydown && !this._keys_down.length && (key === "alt" || key === _metakey)) { - return; + } else { + this._sequence = []; + } + }; + + Listener.prototype._get_sequence = function(key) { + var combo, i, j, match, seq_key, sequence, _i, _j, _k, _len, _ref, _ref1, _ref2; + _ref = this._registered_combos; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + combo = _ref[_i]; + if (!combo.is_sequence) { + continue; } - if (!key) { - return; + for (j = _j = 1, _ref1 = this._sequence.length; 1 <= _ref1 ? _j <= _ref1 : _j >= _ref1; j = 1 <= _ref1 ? ++_j : --_j) { + sequence = (_filter_array(this._sequence, function(seq_key) { + if (__indexOf.call(combo.keys, "shift") >= 0) { + return true; + } + return seq_key !== "shift"; + })).slice(-j); + if (combo.keys.length !== sequence.length) { + continue; + } + for (i = _k = 0, _ref2 = sequence.length; 0 <= _ref2 ? _k < _ref2 : _k > _ref2; i = 0 <= _ref2 ? ++_k : --_k) { + seq_key = sequence[i]; + if (__indexOf.call(combo.keys, "shift") < 0) { + if (seq_key === "shift") { + continue; + } + } + if (key === "shift" && __indexOf.call(combo.keys, "shift") < 0) { + continue; + } + if (combo.keys[i] === seq_key) { + match = true; + } else { + match = false; + break; + } + } } - if (is_keydown) { - return this._key_down(key, e); - } else { - return this._key_up(key, e); + if (match) { + if (combo.is_exclusive) { + this._sequence = []; + } + return combo; } - }; + } + return false; + }; - Listener.prototype._fire = function(event, combo, key_event, is_autorepeat) { - if (typeof combo["on_" + event] === "function") { - this._prevent_default(key_event, combo["on_" + event].call(combo["this"], key_event, combo.count, is_autorepeat) !== true); - } - if (event === "release") { - combo.count = 0; - } - if (event === "keyup") { - return combo.keyup_fired = true; + Listener.prototype._receive_input = function(e, is_keydown) { + var key, _ref; + if (this._prevent_capture) { + if (this._keys_down.length) { + this._keys_down = []; } - }; + return; + } + key = _convert_key_to_readable((_ref = e.keyCode) != null ? _ref : e.key); + if (!is_keydown && !this._keys_down.length && (key === "alt" || key === _metakey)) { + return; + } + if (!key) { + return; + } + if (is_keydown) { + return this._key_down(key, e); + } else { + return this._key_up(key, e); + } + }; - Listener.prototype._match_combo_arrays = function(potential_match, match_handler) { - var combo_potential_match, l, len, ref, source_combo; - ref = this._registered_combos; - for (l = 0, len = ref.length; l < len; l++) { - source_combo = ref[l]; - combo_potential_match = potential_match.slice(0); - if (source_combo.normalize_caps_lock && indexOf.call(combo_potential_match, "caps") >= 0) { - combo_potential_match.splice(combo_potential_match.indexOf("caps"), 1); - } - if ((!source_combo.is_unordered && _compare_arrays_sorted(combo_potential_match, source_combo.keys)) || (source_combo.is_unordered && _compare_arrays(combo_potential_match, source_combo.keys))) { - match_handler(source_combo); - } + Listener.prototype._fire = function(event, combo, key_event, is_autorepeat) { + if (typeof combo["on_" + event] === "function") { + this._prevent_default(key_event, combo["on_" + event].call(combo["this"], key_event, combo.count, is_autorepeat) !== true); + } + if (event === "release") { + combo.count = 0; + } + if (event === "keyup") { + return combo.keyup_fired = true; + } + }; + + Listener.prototype._match_combo_arrays = function(potential_match, match_handler) { + var combo_potential_match, source_combo, _i, _len, _ref; + _ref = this._registered_combos; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + source_combo = _ref[_i]; + combo_potential_match = potential_match.slice(0); + if (source_combo.normalize_caps_lock && __indexOf.call(combo_potential_match, "caps") >= 0) { + combo_potential_match.splice(combo_potential_match.indexOf("caps"), 1); } - }; + if ((!source_combo.is_unordered && _compare_arrays_sorted(combo_potential_match, source_combo.keys)) || (source_combo.is_unordered && _compare_arrays(combo_potential_match, source_combo.keys))) { + match_handler(source_combo); + } + } + }; - Listener.prototype._fuzzy_match_combo_arrays = function(potential_match, match_handler) { - var l, len, ref, source_combo; - ref = this._registered_combos; - for (l = 0, len = ref.length; l < len; l++) { - source_combo = ref[l]; - if ((!source_combo.is_unordered && _is_array_in_array_sorted(source_combo.keys, potential_match)) || (source_combo.is_unordered && _is_array_in_array(source_combo.keys, potential_match))) { - match_handler(source_combo); - } + Listener.prototype._fuzzy_match_combo_arrays = function(potential_match, match_handler) { + var source_combo, _i, _len, _ref; + _ref = this._registered_combos; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + source_combo = _ref[_i]; + if ((!source_combo.is_unordered && _is_array_in_array_sorted(source_combo.keys, potential_match)) || (source_combo.is_unordered && _is_array_in_array(source_combo.keys, potential_match))) { + match_handler(source_combo); } - }; + } + }; - Listener.prototype._keys_remain = function(combo) { - var key, keys_remain, l, len, ref; - ref = combo.keys; - for (l = 0, len = ref.length; l < len; l++) { - key = ref[l]; - if (indexOf.call(this._keys_down, key) >= 0) { - keys_remain = true; - break; - } + Listener.prototype._keys_remain = function(combo) { + var key, keys_remain, _i, _len, _ref; + _ref = combo.keys; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + key = _ref[_i]; + if (__indexOf.call(this._keys_down, key) >= 0) { + keys_remain = true; + break; } - return keys_remain; - }; + } + return keys_remain; + }; - Listener.prototype._key_down = function(key, e) { - var combo, combos, event_mod, i, l, len, len1, m, mod, n, potential, potential_combos, ref, sequence_combo, shifted_key; - shifted_key = _convert_to_shifted_key(key, e); - if (shifted_key) { - key = shifted_key; + Listener.prototype._key_down = function(key, e) { + var combo, combos, event_mod, i, mod, potential, potential_combos, sequence_combo, shifted_key, _i, _j, _k, _len, _len1, _ref; + shifted_key = _convert_to_shifted_key(key, e); + if (shifted_key) { + key = shifted_key; + } + this._add_key_to_sequence(key, e); + sequence_combo = this._get_sequence(key); + if (sequence_combo) { + this._fire("keydown", sequence_combo, e); + } + for (mod in _modifier_event_mapping) { + event_mod = _modifier_event_mapping[mod]; + if (!e[event_mod]) { + continue; } - this._add_key_to_sequence(key, e); - sequence_combo = this._get_sequence(key); - if (sequence_combo) { - this._fire("keydown", sequence_combo, e); + if (mod === key || __indexOf.call(this._keys_down, mod) >= 0) { + continue; } - for (mod in _modifier_event_mapping) { - event_mod = _modifier_event_mapping[mod]; - if (!e[event_mod]) { - continue; - } - if (mod === key || indexOf.call(this._keys_down, mod) >= 0) { - continue; - } - this._keys_down.push(mod); + this._keys_down.push(mod); + } + for (mod in _modifier_event_mapping) { + event_mod = _modifier_event_mapping[mod]; + if (mod === key) { + continue; } - for (mod in _modifier_event_mapping) { - event_mod = _modifier_event_mapping[mod]; - if (mod === key) { + if (__indexOf.call(this._keys_down, mod) >= 0 && !e[event_mod]) { + if (mod === "cmd" && _metakey !== "cmd") { continue; } - if (indexOf.call(this._keys_down, mod) >= 0 && !e[event_mod]) { - if (mod === "cmd" && _metakey !== "cmd") { - continue; + for (i = _i = 0, _ref = this._keys_down.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) { + if (this._keys_down[i] === mod) { + this._keys_down.splice(i, 1); } - for (i = l = 0, ref = this._keys_down.length; 0 <= ref ? l < ref : l > ref; i = 0 <= ref ? ++l : --l) { - if (this._keys_down[i] === mod) { - this._keys_down.splice(i, 1); - } - } - } - } - combos = this._get_active_combos(key); - potential_combos = this._get_potential_combos(key); - for (m = 0, len = combos.length; m < len; m++) { - combo = combos[m]; - this._handle_combo_down(combo, potential_combos, key, e); - } - if (potential_combos.length) { - for (n = 0, len1 = potential_combos.length; n < len1; n++) { - potential = potential_combos[n]; - this._prevent_default(e, potential.prevent_default); } } - if (indexOf.call(this._keys_down, key) < 0) { - this._keys_down.push(key); + } + combos = this._get_active_combos(key); + potential_combos = this._get_potential_combos(key); + for (_j = 0, _len = combos.length; _j < _len; _j++) { + combo = combos[_j]; + this._handle_combo_down(combo, potential_combos, key, e); + } + if (potential_combos.length) { + for (_k = 0, _len1 = potential_combos.length; _k < _len1; _k++) { + potential = potential_combos[_k]; + this._prevent_default(e, potential.prevent_default); } - }; + } + if (__indexOf.call(this._keys_down, key) < 0) { + this._keys_down.push(key); + } + }; - Listener.prototype._handle_combo_down = function(combo, potential_combos, key, e) { - var is_autorepeat, is_other_exclusive, l, len, potential_combo, result; - if (indexOf.call(combo.keys, key) < 0) { + Listener.prototype._handle_combo_down = function(combo, potential_combos, key, e) { + var is_autorepeat, is_other_exclusive, potential_combo, result, _i, _len; + if (__indexOf.call(combo.keys, key) < 0) { + return false; + } + this._prevent_default(e, combo && combo.prevent_default); + is_autorepeat = false; + if (__indexOf.call(this._keys_down, key) >= 0) { + is_autorepeat = true; + if (!combo.allows_key_repeat()) { return false; } - this._prevent_default(e, combo && combo.prevent_default); - is_autorepeat = false; - if (indexOf.call(this._keys_down, key) >= 0) { - is_autorepeat = true; - if (!combo.allows_key_repeat()) { - return false; + } + result = this._add_to_active_combos(combo, key); + combo.keyup_fired = false; + is_other_exclusive = false; + if (combo.is_exclusive) { + for (_i = 0, _len = potential_combos.length; _i < _len; _i++) { + potential_combo = potential_combos[_i]; + if (potential_combo.is_exclusive && potential_combo.keys.length > combo.keys.length) { + is_other_exclusive = true; + break; } } - result = this._add_to_active_combos(combo, key); - combo.keyup_fired = false; - is_other_exclusive = false; - if (combo.is_exclusive) { - for (l = 0, len = potential_combos.length; l < len; l++) { - potential_combo = potential_combos[l]; - if (potential_combo.is_exclusive && potential_combo.keys.length > combo.keys.length) { - is_other_exclusive = true; - break; - } - } + } + if (!is_other_exclusive) { + if (combo.is_counting && typeof combo.on_keydown === "function") { + combo.count += 1; } - if (!is_other_exclusive) { - if (combo.is_counting && typeof combo.on_keydown === "function") { - combo.count += 1; - } - if (result) { - return this._fire("keydown", combo, e, is_autorepeat); - } + if (result) { + return this._fire("keydown", combo, e, is_autorepeat); } - }; + } + }; - Listener.prototype._key_up = function(key, e) { - var active_combo, active_combos_length, combo, combos, i, l, len, len1, len2, m, n, o, ref, ref1, ref2, ref3, sequence_combo, shifted_key, unshifted_key; - unshifted_key = key; - shifted_key = _convert_to_shifted_key(key, e); - if (shifted_key) { - key = shifted_key; - } - shifted_key = _keycode_shifted_keys[unshifted_key]; - if (e.shiftKey) { - if (!(shifted_key && indexOf.call(this._keys_down, shifted_key) >= 0)) { - key = unshifted_key; - } - } else { - if (!(unshifted_key && indexOf.call(this._keys_down, unshifted_key) >= 0)) { - key = shifted_key; - } + Listener.prototype._key_up = function(key, e) { + var active_combo, active_combos_length, combo, combos, i, sequence_combo, shifted_key, unshifted_key, _i, _j, _k, _l, _len, _len1, _len2, _ref, _ref1, _ref2, _ref3; + unshifted_key = key; + shifted_key = _convert_to_shifted_key(key, e); + if (shifted_key) { + key = shifted_key; + } + shifted_key = _keycode_shifted_keys[unshifted_key]; + if (e.shiftKey) { + if (!(shifted_key && __indexOf.call(this._keys_down, shifted_key) >= 0)) { + key = unshifted_key; } - sequence_combo = this._get_sequence(key); - if (sequence_combo) { - this._fire("keyup", sequence_combo, e); + } else { + if (!(unshifted_key && __indexOf.call(this._keys_down, unshifted_key) >= 0)) { + key = shifted_key; } - if (indexOf.call(this._keys_down, key) < 0) { - return false; + } + sequence_combo = this._get_sequence(key); + if (sequence_combo) { + this._fire("keyup", sequence_combo, e); + } + if (__indexOf.call(this._keys_down, key) < 0) { + return false; + } + for (i = _i = 0, _ref = this._keys_down.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) { + if ((_ref1 = this._keys_down[i]) === key || _ref1 === shifted_key || _ref1 === unshifted_key) { + this._keys_down.splice(i, 1); + break; } - for (i = l = 0, ref = this._keys_down.length; 0 <= ref ? l < ref : l > ref; i = 0 <= ref ? ++l : --l) { - if ((ref1 = this._keys_down[i]) === key || ref1 === shifted_key || ref1 === unshifted_key) { - this._keys_down.splice(i, 1); - break; - } + } + active_combos_length = this._active_combos.length; + combos = []; + _ref2 = this._active_combos; + for (_j = 0, _len = _ref2.length; _j < _len; _j++) { + active_combo = _ref2[_j]; + if (__indexOf.call(active_combo.keys, key) >= 0) { + combos.push(active_combo); } - active_combos_length = this._active_combos.length; - combos = []; - ref2 = this._active_combos; - for (m = 0, len = ref2.length; m < len; m++) { - active_combo = ref2[m]; - if (indexOf.call(active_combo.keys, key) >= 0) { - combos.push(active_combo); + } + for (_k = 0, _len1 = combos.length; _k < _len1; _k++) { + combo = combos[_k]; + this._handle_combo_up(combo, e, key); + } + if (active_combos_length > 1) { + _ref3 = this._active_combos; + for (_l = 0, _len2 = _ref3.length; _l < _len2; _l++) { + active_combo = _ref3[_l]; + if (active_combo === void 0 || __indexOf.call(combos, active_combo) >= 0) { + continue; } - } - for (n = 0, len1 = combos.length; n < len1; n++) { - combo = combos[n]; - this._handle_combo_up(combo, e, key); - } - if (active_combos_length > 1) { - ref3 = this._active_combos; - for (o = 0, len2 = ref3.length; o < len2; o++) { - active_combo = ref3[o]; - if (active_combo === void 0 || indexOf.call(combos, active_combo) >= 0) { - continue; - } - if (!this._keys_remain(active_combo)) { - this._remove_from_active_combos(active_combo); - } + if (!this._keys_remain(active_combo)) { + this._remove_from_active_combos(active_combo); } } - }; + } + }; - Listener.prototype._handle_combo_up = function(combo, e, key) { - var keys_down, keys_remaining; - this._prevent_default(e, combo && combo.prevent_default); - keys_remaining = this._keys_remain(combo); - if (!combo.keyup_fired) { - keys_down = this._keys_down.slice(); - keys_down.push(key); - if (!combo.is_solitary || _compare_arrays(keys_down, combo.keys)) { - this._fire("keyup", combo, e); - if (combo.is_counting && typeof combo.on_keyup === "function" && typeof combo.on_keydown !== "function") { - combo.count += 1; - } + Listener.prototype._handle_combo_up = function(combo, e, key) { + var keys_down, keys_remaining; + this._prevent_default(e, combo && combo.prevent_default); + keys_remaining = this._keys_remain(combo); + if (!combo.keyup_fired) { + keys_down = this._keys_down.slice(); + keys_down.push(key); + if (!combo.is_solitary || _compare_arrays(keys_down, combo.keys)) { + this._fire("keyup", combo, e); + if (combo.is_counting && typeof combo.on_keyup === "function" && typeof combo.on_keydown !== "function") { + combo.count += 1; } } - if (!keys_remaining) { - this._fire("release", combo, e); - this._remove_from_active_combos(combo); - } - }; + } + if (!keys_remaining) { + this._fire("release", combo, e); + this._remove_from_active_combos(combo); + } + }; - Listener.prototype.simple_combo = function(keys, callback) { - return this.register_combo({ - keys: keys, - on_keydown: callback - }); - }; + Listener.prototype.simple_combo = function(keys, callback) { + return this.register_combo({ + keys: keys, + on_keydown: callback + }); + }; - Listener.prototype.counting_combo = function(keys, count_callback) { - return this.register_combo({ - keys: keys, - is_counting: true, - is_unordered: false, - on_keydown: count_callback - }); - }; + Listener.prototype.counting_combo = function(keys, count_callback) { + return this.register_combo({ + keys: keys, + is_counting: true, + is_unordered: false, + on_keydown: count_callback + }); + }; - Listener.prototype.sequence_combo = function(keys, callback) { - return this.register_combo({ - keys: keys, - on_keydown: callback, - is_sequence: true, - is_exclusive: true - }); - }; + Listener.prototype.sequence_combo = function(keys, callback) { + return this.register_combo({ + keys: keys, + on_keydown: callback, + is_sequence: true, + is_exclusive: true + }); + }; - Listener.prototype.register_combo = function(combo_dictionary) { - var combo, property, ref, value; - if (typeof combo_dictionary["keys"] === "string") { - combo_dictionary["keys"] = combo_dictionary["keys"].split(" "); - } - ref = this._defaults; - for (property in ref) { - if (!hasProp.call(ref, property)) continue; - value = ref[property]; - if (combo_dictionary[property] === void 0) { - combo_dictionary[property] = value; - } - } - combo = new Combo(combo_dictionary); - if (_validate_combo(combo)) { - this._registered_combos.push(combo); - return combo; + Listener.prototype.register_combo = function(combo_dictionary) { + var combo, property, value, _ref; + if (typeof combo_dictionary["keys"] === "string") { + combo_dictionary["keys"] = combo_dictionary["keys"].split(" "); + } + _ref = this._defaults; + for (property in _ref) { + if (!__hasProp.call(_ref, property)) continue; + value = _ref[property]; + if (combo_dictionary[property] === void 0) { + combo_dictionary[property] = value; } - }; + } + combo = new Combo(combo_dictionary); + if (_validate_combo(combo)) { + this._registered_combos.push(combo); + return combo; + } + }; - Listener.prototype.register_many = function(combo_array) { - var combo, l, len, results; - results = []; - for (l = 0, len = combo_array.length; l < len; l++) { - combo = combo_array[l]; - results.push(this.register_combo(combo)); - } - return results; - }; + Listener.prototype.register_many = function(combo_array) { + var combo, _i, _len, _results; + _results = []; + for (_i = 0, _len = combo_array.length; _i < _len; _i++) { + combo = combo_array[_i]; + _results.push(this.register_combo(combo)); + } + return _results; + }; - Listener.prototype.unregister_combo = function(keys_or_combo) { - var combo, i, l, len, m, ref, ref1, results, unregister_combo; - if (!keys_or_combo) { - return false; - } - unregister_combo = (function(_this) { - return function(combo) { - var i, l, ref, results; - results = []; - for (i = l = 0, ref = _this._registered_combos.length; 0 <= ref ? l < ref : l > ref; i = 0 <= ref ? ++l : --l) { - if (combo === _this._registered_combos[i]) { - _this._registered_combos.splice(i, 1); - break; - } else { - results.push(void 0); - } - } - return results; - }; - })(this); - if (keys_or_combo instanceof Combo) { - return unregister_combo(keys_or_combo); - } else { - if (typeof keys_or_combo === "string") { - keys_or_combo = keys_or_combo.split(" "); - for (i = l = 0, ref = keys_or_combo.length; 0 <= ref ? l < ref : l > ref; i = 0 <= ref ? ++l : --l) { - if (keys_or_combo[i] === "meta") { - keys_or_combo[i] = _metakey; - } + Listener.prototype.unregister_combo = function(keys_or_combo) { + var combo, i, unregister_combo, _i, _j, _len, _ref, _ref1, _results; + if (!keys_or_combo) { + return false; + } + unregister_combo = (function(_this) { + return function(combo) { + var i, _i, _ref, _results; + _results = []; + for (i = _i = 0, _ref = _this._registered_combos.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) { + if (combo === _this._registered_combos[i]) { + _this._registered_combos.splice(i, 1); + break; + } else { + _results.push(void 0); } } - ref1 = this._registered_combos; - results = []; - for (m = 0, len = ref1.length; m < len; m++) { - combo = ref1[m]; - if (combo == null) { - continue; - } - if ((combo.is_unordered && _compare_arrays(keys_or_combo, combo.keys)) || (!combo.is_unordered && _compare_arrays_sorted(keys_or_combo, combo.keys))) { - results.push(unregister_combo(combo)); - } else { - results.push(void 0); + return _results; + }; + })(this); + if (keys_or_combo instanceof Combo) { + return unregister_combo(keys_or_combo); + } else { + if (typeof keys_or_combo === "string") { + keys_or_combo = keys_or_combo.split(" "); + for (i = _i = 0, _ref = keys_or_combo.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) { + if (keys_or_combo[i] === "meta") { + keys_or_combo[i] = _metakey; } } - return results; } - }; - - Listener.prototype.unregister_many = function(combo_array) { - var combo, l, len, results; - results = []; - for (l = 0, len = combo_array.length; l < len; l++) { - combo = combo_array[l]; - results.push(this.unregister_combo(combo)); + _ref1 = this._registered_combos; + _results = []; + for (_j = 0, _len = _ref1.length; _j < _len; _j++) { + combo = _ref1[_j]; + if (combo == null) { + continue; + } + if ((combo.is_unordered && _compare_arrays(keys_or_combo, combo.keys)) || (!combo.is_unordered && _compare_arrays_sorted(keys_or_combo, combo.keys))) { + _results.push(unregister_combo(combo)); + } else { + _results.push(void 0); + } } - return results; - }; - - Listener.prototype.get_registered_combos = function() { - return this._registered_combos; - }; - - Listener.prototype.reset = function() { - return this._registered_combos = []; - }; - - Listener.prototype.listen = function() { - return this._prevent_capture = false; - }; - - Listener.prototype.stop_listening = function() { - return this._prevent_capture = true; - }; - - Listener.prototype.get_meta_key = function() { - return _metakey; - }; - - return Listener; - - })(); - - _decide_meta_key = function() { - if (navigator.userAgent.indexOf("Mac OS X") !== -1) { - _metakey = "cmd"; + return _results; } }; - _change_keycodes_by_browser = function() { - if (navigator.userAgent.indexOf("Opera") !== -1) { - _keycode_dictionary["17"] = "cmd"; + Listener.prototype.unregister_many = function(combo_array) { + var combo, _i, _len, _results; + _results = []; + for (_i = 0, _len = combo_array.length; _i < _len; _i++) { + combo = combo_array[_i]; + _results.push(this.unregister_combo(combo)); } + return _results; }; - _convert_key_to_readable = function(k) { - return _keycode_dictionary[k]; + Listener.prototype.get_registered_combos = function() { + return this._registered_combos; }; - _filter_array = function(array, callback) { - var element; - if (array.filter) { - return array.filter(callback); - } else { - return (function() { - var l, len, results; - results = []; - for (l = 0, len = array.length; l < len; l++) { - element = array[l]; - if (callback(element)) { - results.push(element); - } - } - return results; - })(); - } + Listener.prototype.reset = function() { + return this._registered_combos = []; }; - _compare_arrays = function(a1, a2) { - var item, l, len; - if (a1.length !== a2.length) { - return false; - } - for (l = 0, len = a1.length; l < len; l++) { - item = a1[l]; - if (indexOf.call(a2, item) >= 0) { - continue; - } - return false; - } - return true; + Listener.prototype.listen = function() { + return this._prevent_capture = false; }; - _compare_arrays_sorted = function(a1, a2) { - var i, l, ref; - if (a1.length !== a2.length) { - return false; - } - for (i = l = 0, ref = a1.length; 0 <= ref ? l < ref : l > ref; i = 0 <= ref ? ++l : --l) { - if (a1[i] !== a2[i]) { - return false; - } - } - return true; + Listener.prototype.stop_listening = function() { + return this._prevent_capture = true; }; - _is_array_in_array = function(a1, a2) { - var item, l, len; - for (l = 0, len = a1.length; l < len; l++) { - item = a1[l]; - if (indexOf.call(a2, item) < 0) { - return false; - } - } - return true; + Listener.prototype.get_meta_key = function() { + return _metakey; }; - _index_of_in_array = Array.prototype.indexOf || function(a, item) { - var i, l, ref; - for (i = l = 0, ref = a.length; 0 <= ref ? l <= ref : l >= ref; i = 0 <= ref ? ++l : --l) { - if (a[i] === item) { - return i; - } - } - return -1; - }; + return Listener; - _is_array_in_array_sorted = function(a1, a2) { - var index, item, l, len, prev; - prev = 0; - for (l = 0, len = a1.length; l < len; l++) { - item = a1[l]; - index = _index_of_in_array.call(a2, item); - if (index >= prev) { - prev = index; - } else { - return false; +})(); + +_decide_meta_key = function() { + if (navigator.userAgent.indexOf("Mac OS X") !== -1) { + _metakey = "cmd"; + } +}; + +_change_keycodes_by_browser = function() { + if (navigator.userAgent.indexOf("Opera") !== -1) { + _keycode_dictionary["17"] = "cmd"; + } +}; + +_convert_key_to_readable = function(k) { + return _keycode_dictionary[k]; +}; + +_filter_array = function(array, callback) { + var element; + if (array.filter) { + return array.filter(callback); + } else { + return (function() { + var _i, _len, _results; + _results = []; + for (_i = 0, _len = array.length; _i < _len; _i++) { + element = array[_i]; + if (callback(element)) { + _results.push(element); + } } + return _results; + })(); + } +}; + +_compare_arrays = function(a1, a2) { + var item, _i, _len; + if (a1.length !== a2.length) { + return false; + } + for (_i = 0, _len = a1.length; _i < _len; _i++) { + item = a1[_i]; + if (__indexOf.call(a2, item) >= 0) { + continue; } - return true; - }; + return false; + } + return true; +}; - _log_error = function() { - if (keypress.debug) { - return console.log.apply(console, arguments); +_compare_arrays_sorted = function(a1, a2) { + var i, _i, _ref; + if (a1.length !== a2.length) { + return false; + } + for (i = _i = 0, _ref = a1.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) { + if (a1[i] !== a2[i]) { + return false; } - }; + } + return true; +}; + +_is_array_in_array = function(a1, a2) { + var item, _i, _len; + for (_i = 0, _len = a1.length; _i < _len; _i++) { + item = a1[_i]; + if (__indexOf.call(a2, item) < 0) { + return false; + } + } + return true; +}; + +_index_of_in_array = Array.prototype.indexOf || function(a, item) { + var i, _i, _ref; + for (i = _i = 0, _ref = a.length; 0 <= _ref ? _i <= _ref : _i >= _ref; i = 0 <= _ref ? ++_i : --_i) { + if (a[i] === item) { + return i; + } + } + return -1; +}; + +_is_array_in_array_sorted = function(a1, a2) { + var index, item, prev, _i, _len; + prev = 0; + for (_i = 0, _len = a1.length; _i < _len; _i++) { + item = a1[_i]; + index = _index_of_in_array.call(a2, item); + if (index >= prev) { + prev = index; + } else { + return false; + } + } + return true; +}; - _key_is_valid = function(key) { - var _, valid, valid_key; - valid = false; - for (_ in _keycode_dictionary) { - valid_key = _keycode_dictionary[_]; +_log_error = function() { + if (keypress.debug) { + return console.log.apply(console, arguments); + } +}; + +_key_is_valid = function(key) { + var valid, valid_key, _; + valid = false; + for (_ in _keycode_dictionary) { + valid_key = _keycode_dictionary[_]; + if (key === valid_key) { + valid = true; + break; + } + } + if (!valid) { + for (_ in _keycode_shifted_keys) { + valid_key = _keycode_shifted_keys[_]; if (key === valid_key) { valid = true; break; } } - if (!valid) { - for (_ in _keycode_shifted_keys) { - valid_key = _keycode_shifted_keys[_]; - if (key === valid_key) { - valid = true; - break; - } - } + } + return valid; +}; + +_validate_combo = function(combo) { + var alt_name, i, key, mod_key, non_modifier_keys, property, validated, value, _i, _j, _k, _len, _len1, _ref, _ref1; + validated = true; + if (!combo.keys.length) { + _log_error("You're trying to bind a combo with no keys:", combo); + } + for (i = _i = 0, _ref = combo.keys.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) { + key = combo.keys[i]; + alt_name = _keycode_alternate_names[key]; + if (alt_name) { + key = combo.keys[i] = alt_name; } - return valid; - }; - - _validate_combo = function(combo) { - var alt_name, i, key, l, len, len1, m, mod_key, n, non_modifier_keys, property, ref, ref1, validated, value; - validated = true; - if (!combo.keys.length) { - _log_error("You're trying to bind a combo with no keys:", combo); - } - for (i = l = 0, ref = combo.keys.length; 0 <= ref ? l < ref : l > ref; i = 0 <= ref ? ++l : --l) { - key = combo.keys[i]; - alt_name = _keycode_alternate_names[key]; - if (alt_name) { - key = combo.keys[i] = alt_name; - } - if (key === "meta") { - combo.keys.splice(i, 1, _metakey); - } - if (key === "cmd") { - _log_error("Warning: use the \"meta\" key rather than \"cmd\" for Windows compatibility"); - } + if (key === "meta") { + combo.keys.splice(i, 1, _metakey); } - ref1 = combo.keys; - for (m = 0, len = ref1.length; m < len; m++) { - key = ref1[m]; - if (!_key_is_valid(key)) { - _log_error("Do not recognize the key \"" + key + "\""); - validated = false; - } + if (key === "cmd") { + _log_error("Warning: use the \"meta\" key rather than \"cmd\" for Windows compatibility"); } - if (indexOf.call(combo.keys, "meta") >= 0 || indexOf.call(combo.keys, "cmd") >= 0) { - non_modifier_keys = combo.keys.slice(); - for (n = 0, len1 = _modifier_keys.length; n < len1; n++) { - mod_key = _modifier_keys[n]; - if ((i = _index_of_in_array.call(non_modifier_keys, mod_key)) > -1) { - non_modifier_keys.splice(i, 1); - } - } - if (non_modifier_keys.length > 1) { - _log_error("META and CMD key combos cannot have more than 1 non-modifier keys", combo, non_modifier_keys); - validated = false; - } + } + _ref1 = combo.keys; + for (_j = 0, _len = _ref1.length; _j < _len; _j++) { + key = _ref1[_j]; + if (!_key_is_valid(key)) { + _log_error("Do not recognize the key \"" + key + "\""); + validated = false; } - for (property in combo) { - value = combo[property]; - if (_factory_defaults[property] === "undefined") { - _log_error("The property " + property + " is not a valid combo property. Your combo has still been registered."); + } + if (__indexOf.call(combo.keys, "meta") >= 0 || __indexOf.call(combo.keys, "cmd") >= 0) { + non_modifier_keys = combo.keys.slice(); + for (_k = 0, _len1 = _modifier_keys.length; _k < _len1; _k++) { + mod_key = _modifier_keys[_k]; + if ((i = _index_of_in_array.call(non_modifier_keys, mod_key)) > -1) { + non_modifier_keys.splice(i, 1); } } - return validated; - }; - - _convert_to_shifted_key = function(key, e) { - var k; - if (!e.shiftKey) { - return false; + if (non_modifier_keys.length > 1) { + _log_error("META and CMD key combos cannot have more than 1 non-modifier keys", combo, non_modifier_keys); + validated = false; } - k = _keycode_shifted_keys[key]; - if (k != null) { - return k; + } + for (property in combo) { + value = combo[property]; + if (_factory_defaults[property] === "undefined") { + _log_error("The property " + property + " is not a valid combo property. Your combo has still been registered."); } - return false; - }; - - _modifier_event_mapping = { - "cmd": "metaKey", - "ctrl": "ctrlKey", - "shift": "shiftKey", - "alt": "altKey" - }; - - _keycode_alternate_names = { - "escape": "esc", - "control": "ctrl", - "command": "cmd", - "break": "pause", - "windows": "cmd", - "option": "alt", - "caps_lock": "caps", - "apostrophe": "\'", - "semicolon": ";", - "tilde": "~", - "accent": "`", - "scroll_lock": "scroll", - "num_lock": "num" - }; - - _keycode_shifted_keys = { - "/": "?", - ".": ">", - ",": "<", - "\'": "\"", - ";": ":", - "[": "{", - "]": "}", - "\\": "|", - "`": "~", - "=": "+", - "-": "_", - "1": "!", - "2": "@", - "3": "#", - "4": "$", - "5": "%", - "6": "^", - "7": "&", - "8": "*", - "9": "(", - "0": ")" - }; - - _keycode_dictionary = { - 0: "\\", - 8: "backspace", - 9: "tab", - 12: "num", - 13: "enter", - 16: "shift", - 17: "ctrl", - 18: "alt", - 19: "pause", - 20: "caps", - 27: "esc", - 32: "space", - 33: "pageup", - 34: "pagedown", - 35: "end", - 36: "home", - 37: "left", - 38: "up", - 39: "right", - 40: "down", - 44: "print", - 45: "insert", - 46: "delete", - 48: "0", - 49: "1", - 50: "2", - 51: "3", - 52: "4", - 53: "5", - 54: "6", - 55: "7", - 56: "8", - 57: "9", - 65: "a", - 66: "b", - 67: "c", - 68: "d", - 69: "e", - 70: "f", - 71: "g", - 72: "h", - 73: "i", - 74: "j", - 75: "k", - 76: "l", - 77: "m", - 78: "n", - 79: "o", - 80: "p", - 81: "q", - 82: "r", - 83: "s", - 84: "t", - 85: "u", - 86: "v", - 87: "w", - 88: "x", - 89: "y", - 90: "z", - 91: "cmd", - 92: "cmd", - 93: "cmd", - 96: "num_0", - 97: "num_1", - 98: "num_2", - 99: "num_3", - 100: "num_4", - 101: "num_5", - 102: "num_6", - 103: "num_7", - 104: "num_8", - 105: "num_9", - 106: "num_multiply", - 107: "num_add", - 108: "num_enter", - 109: "num_subtract", - 110: "num_decimal", - 111: "num_divide", - 112: "f1", - 113: "f2", - 114: "f3", - 115: "f4", - 116: "f5", - 117: "f6", - 118: "f7", - 119: "f8", - 120: "f9", - 121: "f10", - 122: "f11", - 123: "f12", - 124: "print", - 144: "num", - 145: "scroll", - 186: ";", - 187: "=", - 188: ",", - 189: "-", - 190: ".", - 191: "/", - 192: "`", - 219: "[", - 220: "\\", - 221: "]", - 222: "\'", - 223: "`", - 224: "cmd", - 225: "alt", - 57392: "ctrl", - 63289: "num", - 59: ";", - 61: "=", - 173: "-" - }; - - keypress._keycode_dictionary = _keycode_dictionary; - - keypress._is_array_in_array_sorted = _is_array_in_array_sorted; - - _decide_meta_key(); - - _change_keycodes_by_browser(); - - if (typeof define === "function" && define.amd) { - define([], function() { - return keypress; - }); - } else if (typeof exports !== "undefined" && exports !== null) { - exports.keypress = keypress; - } else { - window.keypress = keypress; } + return validated; +}; -}).call(this); +_convert_to_shifted_key = function(key, e) { + var k; + if (!e.shiftKey) { + return false; + } + k = _keycode_shifted_keys[key]; + if (k != null) { + return k; + } + return false; +}; + +_modifier_event_mapping = { + "cmd": "metaKey", + "ctrl": "ctrlKey", + "shift": "shiftKey", + "alt": "altKey" +}; + +_keycode_alternate_names = { + "escape": "esc", + "control": "ctrl", + "command": "cmd", + "break": "pause", + "windows": "cmd", + "option": "alt", + "caps_lock": "caps", + "apostrophe": "\'", + "semicolon": ";", + "tilde": "~", + "accent": "`", + "scroll_lock": "scroll", + "num_lock": "num" +}; + +_keycode_shifted_keys = { + "/": "?", + ".": ">", + ",": "<", + "\'": "\"", + ";": ":", + "[": "{", + "]": "}", + "\\": "|", + "`": "~", + "=": "+", + "-": "_", + "1": "!", + "2": "@", + "3": "#", + "4": "$", + "5": "%", + "6": "^", + "7": "&", + "8": "*", + "9": "(", + "0": ")" +}; + +_keycode_dictionary = { + 0: "\\", + 8: "backspace", + 9: "tab", + 12: "num", + 13: "enter", + 16: "shift", + 17: "ctrl", + 18: "alt", + 19: "pause", + 20: "caps", + 27: "esc", + 32: "space", + 33: "pageup", + 34: "pagedown", + 35: "end", + 36: "home", + 37: "left", + 38: "up", + 39: "right", + 40: "down", + 44: "print", + 45: "insert", + 46: "delete", + 48: "0", + 49: "1", + 50: "2", + 51: "3", + 52: "4", + 53: "5", + 54: "6", + 55: "7", + 56: "8", + 57: "9", + 65: "a", + 66: "b", + 67: "c", + 68: "d", + 69: "e", + 70: "f", + 71: "g", + 72: "h", + 73: "i", + 74: "j", + 75: "k", + 76: "l", + 77: "m", + 78: "n", + 79: "o", + 80: "p", + 81: "q", + 82: "r", + 83: "s", + 84: "t", + 85: "u", + 86: "v", + 87: "w", + 88: "x", + 89: "y", + 90: "z", + 91: "cmd", + 92: "cmd", + 93: "cmd", + 96: "num_0", + 97: "num_1", + 98: "num_2", + 99: "num_3", + 100: "num_4", + 101: "num_5", + 102: "num_6", + 103: "num_7", + 104: "num_8", + 105: "num_9", + 106: "num_multiply", + 107: "num_add", + 108: "num_enter", + 109: "num_subtract", + 110: "num_decimal", + 111: "num_divide", + 112: "f1", + 113: "f2", + 114: "f3", + 115: "f4", + 116: "f5", + 117: "f6", + 118: "f7", + 119: "f8", + 120: "f9", + 121: "f10", + 122: "f11", + 123: "f12", + 124: "print", + 144: "num", + 145: "scroll", + 186: ";", + 187: "=", + 188: ",", + 189: "-", + 190: ".", + 191: "/", + 192: "`", + 219: "[", + 220: "\\", + 221: "]", + 222: "\'", + 223: "`", + 224: "cmd", + 225: "alt", + 57392: "ctrl", + 63289: "num", + 59: ";", + 61: "=", + 173: "-" +}; + +keypress._keycode_dictionary = _keycode_dictionary; + +keypress._is_array_in_array_sorted = _is_array_in_array_sorted; + +_decide_meta_key(); + +_change_keycodes_by_browser(); + +if (typeof define === "function" && define.amd) { + define([], function() { + return keypress; + }); +} else if (typeof exports !== "undefined" && exports !== null) { + exports.keypress = keypress; +} else { + window.keypress = keypress; +} diff --git a/test/tests.coffee b/test/tests.coffee index de1e3b7..43fbebe 100644 --- a/test/tests.coffee +++ b/test/tests.coffee @@ -916,3 +916,13 @@ describe "APIs behave as expected:", -> listener.unregister_combo(combo) count = listener.get_registered_combos().length expect(count).toEqual(0) + + it "unregisters a meta combo from a string", -> + listener.register_combo( + keys: "meta c" + ) + count = listener.get_registered_combos().length + expect(count).toEqual(1) + listener.unregister_combo("meta c") + count = listener.get_registered_combos().length + expect(count).toEqual(0) From 9379767d639b1b6d4f15bd356a4815500903a778 Mon Sep 17 00:00:00 2001 From: David Mauro Date: Mon, 5 Mar 2018 08:55:27 -0500 Subject: [PATCH 8/8] Bump to v2.1.5 --- README.md | 6 +- bower.json | 2 +- coffee-compiler.sh | 4 +- keypress-2.1.4.min.js | 33 - keypress-2.1.5.min.js | 33 + keypress.coffee | 2 +- keypress.js | 1929 +++++++++++++++++++++-------------------- package.json | 6 +- 8 files changed, 1012 insertions(+), 1003 deletions(-) delete mode 100644 keypress-2.1.4.min.js create mode 100644 keypress-2.1.5.min.js diff --git a/README.md b/README.md index 69eab42..08acd3e 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ Keypress ======== -Version 2.1.4 +Version 2.1.5 Keypress is a robust keyboard input capturing Javascript utility focused on input for games. For details and documentation, please @@ -15,6 +15,10 @@ released under the Apache License, version 2.0 **What's new** --------------- +2.1.5 + +* Fixed a bug that would prevent unregistering meta combos by name rather than reference. Thanks to [bichotll](https://github.com/bichotll), [aubincleme](https://github.com/aubincleme), and [mhuebert](https://github.com/mhuebert) for help [spotting](https://github.com/dmauro/Keypress/issues/133) and [fixing](https://github.com/dmauro/Keypress/pull/141) the issue. + 2.1.4 * Remove unexpected debugger diff --git a/bower.json b/bower.json index 0643e94..2290e22 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "Keypress", - "main": "keypress-2.1.4.min.js", + "main": "keypress-2.1.5.min.js", "homepage": "https://github.com/dmauro/Keypress", "authors": [ "David Mauro " diff --git a/coffee-compiler.sh b/coffee-compiler.sh index 5be5067..221b04f 100755 --- a/coffee-compiler.sh +++ b/coffee-compiler.sh @@ -1,6 +1,6 @@ #!/bin/bash -VERSION="2.1.4" -YEAR="2016" +VERSION="2.1.5" +YEAR="2018" cd "$( cd "$( dirname "$0" )" && pwd )" coffee -c keypress.coffee java -jar compiler.jar --js keypress.js --js_output_file keypress-$VERSION.min.js diff --git a/keypress-2.1.4.min.js b/keypress-2.1.4.min.js deleted file mode 100644 index 0a18072..0000000 --- a/keypress-2.1.4.min.js +++ /dev/null @@ -1,33 +0,0 @@ -/* - Keypress version 2.1.4 (c) 2016 David Mauro. - Licensed under the Apache License, Version 2.0 - http://www.apache.org/licenses/LICENSE-2.0 -*/ -(function(){var u,x,y,z,r,v,A,E,B,F,G,q,s,p,l,t,C,H,o,D={}.hasOwnProperty,i=[].indexOf||function(a){for(var c=0,b=this.length;ci.call(a,"cmd")?!1:!0};h.prototype._prevent_default=function(a,c){if((c||this.should_suppress_event_defaults)&&!this.should_force_event_defaults)if(a.preventDefault?a.preventDefault(): -a.returnValue=!1,a.stopPropagation)return a.stopPropagation()};h.prototype._get_active_combos=function(a){var c,b;c=[];b=v(this._keys_down,function(b){return b!==a});b.push(a);this._match_combo_arrays(b,function(a){return function(b){if(a._cmd_bug_check(b.keys))return c.push(b)}}(this));this._fuzzy_match_combo_arrays(b,function(a){return function(b){if(!(0<=i.call(c,b))&&!b.is_solitary&&a._cmd_bug_check(b.keys))return c.push(b)}}(this));return c};h.prototype._get_potential_combos=function(a){var c, -b,d,e,f;e=[];f=this._registered_combos;b=0;for(d=f.length;bh;e=0<=h?++f:--f)if((c=this._active_combos[e])&&c.is_exclusive&&a.is_exclusive){c=c.keys;if(!k){n=0;for(g=c.length;ni.call(a.keys,b)){k=!1;break}}if(j&&!k){m=a.keys;n=0;for(g=m.length;ni.call(c,b)){j=!0;break}}k&&(d?(c=this._active_combos.splice(e,1)[0],null!=c&&c.reset()):(c=this._active_combos.splice(e,1,a)[0],null!=c&&c.reset(),d=!0),j=!1)}}j&&this._active_combos.unshift(a);return k||j};h.prototype._remove_from_active_combos=function(a){var c,b,d,e;b=d=0;for(e=this._active_combos.length;0<=e?de;b=0<=e?++d:--d)if(c=this._active_combos[b],c===a){a=this._active_combos.splice(b, -1)[0];a.reset();break}};h.prototype._get_possible_sequences=function(){var a,c,b,d,e,f,g,n,h,m,j,k;g=[];h=this._registered_combos;b=0;for(d=h.length;b=m;c=1<=m?++e:--e)if(k=this._sequence.slice(-c),a.is_sequence){if(0>i.call(a.keys,"shift")&&(k=v(k,function(a){return"shift"!==a}),!k.length))continue;c=n=0;for(j=k.length;0<=j?nj;c=0<=j?++n:--n)if(a.keys[c]===k[c])f=!0;else{f=!1;break}f&&g.push(a)}}return g};h.prototype._add_key_to_sequence= -function(a,c){var b,d,e,f;this._sequence.push(a);f=this._get_possible_sequences();if(f.length){d=0;for(e=f.length;d=m;b=1<=m?++f:--f)if(o=v(this._sequence,function(a){return 0<=i.call(c.keys,"shift")?!0:"shift"!==a}).slice(-b),c.keys.length===o.length){b=h=0;for(j=o.length;0<=j?hj;b=0<=j?++h:--h)if(k=o[b],!(0>i.call(c.keys,"shift")&&"shift"===k)&&!("shift"===a&&0>i.call(c.keys,"shift")))if(c.keys[b]===k)g=!0;else{g=!1;break}}if(g)return c.is_exclusive&&(this._sequence=[]),c}return!1};h.prototype._receive_input=function(a,c){var b,d;if(this._prevent_capture)this._keys_down.length&& -(this._keys_down=[]);else if(b=y(null!=(d=a.keyCode)?d:a.key),(c||this._keys_down.length||!("alt"===b||b===l))&&b)return c?this._key_down(b,a):this._key_up(b,a)};h.prototype._fire=function(a,c,b,d){"function"===typeof c["on_"+a]&&this._prevent_default(b,!0!==c["on_"+a].call(c["this"],b,c.count,d));"release"===a&&(c.count=0);if("keyup"===a)return c.keyup_fired=!0};h.prototype._match_combo_arrays=function(a,c){var b,d,e,f,g;f=this._registered_combos;d=0;for(e=f.length;de;b=0<=e?++d:--d)this._keys_down[b]===g&&this._keys_down.splice(b,1)}d=this._get_active_combos(a);g=this._get_potential_combos(a);f=0;for(e=d.length;fi.call(this._keys_down,a)&&this._keys_down.push(a)};h.prototype._handle_combo_down=function(a,c,b,d){var e,f,g,h,l;if(0>i.call(a.keys,b))return!1;this._prevent_default(d,a&&a.prevent_default);e=!1;if(0<=i.call(this._keys_down,b)&&(e=!0,!a.allows_key_repeat()))return!1;l=this._add_to_active_combos(a,b);b=a.keyup_fired=!1;if(a.is_exclusive){f=0;for(g=c.length;f -a.keys.length){b=!0;break}}if(!b&&(a.is_counting&&"function"===typeof a.on_keydown&&(a.count+=1),l))return this._fire("keydown",a,d,e)};h.prototype._key_up=function(a,c){var b,d,e,f,g,h;d=a;(b=z(a,c))&&(a=b);b=s[d];c.shiftKey?b&&0<=i.call(this._keys_down,b)||(a=d):d&&0<=i.call(this._keys_down,d)||(a=b);(f=this._get_sequence(a))&&this._fire("keyup",f,c);if(0>i.call(this._keys_down,a))return!1;f=g=0;for(h=this._keys_down.length;0<=h?gh;f=0<=h?++g:--g)if((e=this._keys_down[f])===a||e===b||e===d){this._keys_down.splice(f, -1);break}d=this._active_combos.length;e=[];h=this._active_combos;g=0;for(f=h.length;gd;b=0<=d?++c:--c)if(a===h._registered_combos[b]){h._registered_combos.splice(b,1);break}else e.push(void 0);return e};if(a instanceof w)return g(a);if("string"===typeof a){a=a.split(" ");c=b=0;for(d=a.length;0<=d?bd;c=0<=d?++b:--b)"meta"===a[c]&&(a[c]=l)}e=this._registered_combos;f=[];d=0;for(b=e.length;de;b=0<=e?++d:--d)if(a[b]!==c[b])return!1;return!0};E=function(a,c){var b,d,e;d=0;for(e=a.length;di.call(c,b))return!1;return!0};A=Array.prototype.indexOf||function(a,c){var b,d,e;b=d=0; -for(e=a.length;0<=e?d<=e:d>=e;b=0<=e?++d:--d)if(a[b]===c)return b;return-1};B=function(a,c){var b,d,e,f;d=f=0;for(e=a.length;d=f)f=b;else return!1;return!0};p=function(){if(o.debug)return console.log.apply(console,arguments)};F=function(a){var c,b,d;b=!1;for(c in q)if(d=q[c],a===d){b=!0;break}if(!b)for(c in s)if(d=s[c],a===d){b=!0;break}return b};H=function(a){var c,b,d,e,f,g,h;h=!0;a.keys.length||p("You're trying to bind a combo with no keys:",a);b=e=0;for(f=a.keys.length;0<= -f?ef;b=0<=f?++e:--e)d=a.keys[b],(c=G[d])&&(d=a.keys[b]=c),"meta"===d&&a.keys.splice(b,1,l),"cmd"===d&&p('Warning: use the "meta" key rather than "cmd" for Windows compatibility');f=a.keys;e=0;for(c=f.length;e",",":"<","'":'"', -";":":","[":"{","]":"}","\\":"|","`":"~","=":"+","-":"_",1:"!",2:"@",3:"#",4:"$",5:"%",6:"^",7:"&",8:"*",9:"(","0":")"};q={"0":"\\",8:"backspace",9:"tab",12:"num",13:"enter",16:"shift",17:"ctrl",18:"alt",19:"pause",20:"caps",27:"esc",32:"space",33:"pageup",34:"pagedown",35:"end",36:"home",37:"left",38:"up",39:"right",40:"down",44:"print",45:"insert",46:"delete",48:"0",49:"1",50:"2",51:"3",52:"4",53:"5",54:"6",55:"7",56:"8",57:"9",65:"a",66:"b",67:"c",68:"d",69:"e",70:"f",71:"g",72:"h",73:"i",74:"j", -75:"k",76:"l",77:"m",78:"n",79:"o",80:"p",81:"q",82:"r",83:"s",84:"t",85:"u",86:"v",87:"w",88:"x",89:"y",90:"z",91:"cmd",92:"cmd",93:"cmd",96:"num_0",97:"num_1",98:"num_2",99:"num_3",100:"num_4",101:"num_5",102:"num_6",103:"num_7",104:"num_8",105:"num_9",106:"num_multiply",107:"num_add",108:"num_enter",109:"num_subtract",110:"num_decimal",111:"num_divide",112:"f1",113:"f2",114:"f3",115:"f4",116:"f5",117:"f6",118:"f7",119:"f8",120:"f9",121:"f10",122:"f11",123:"f12",124:"print",144:"num",145:"scroll", -186:";",187:"=",188:",",189:"-",190:".",191:"/",192:"`",219:"[",220:"\\",221:"]",222:"'",223:"`",224:"cmd",225:"alt",57392:"ctrl",63289:"num",59:";",61:"=",173:"-"};o._keycode_dictionary=q;o._is_array_in_array_sorted=B;-1!==navigator.userAgent.indexOf("Mac OS X")&&(l="cmd");-1!==navigator.userAgent.indexOf("Opera")&&(q["17"]="cmd");"function"===typeof define&&define.amd?define([],function(){return o}):"undefined"!==typeof exports&&null!==exports?exports.keypress=o:window.keypress=o}).call(this); diff --git a/keypress-2.1.5.min.js b/keypress-2.1.5.min.js new file mode 100644 index 0000000..05cec4f --- /dev/null +++ b/keypress-2.1.5.min.js @@ -0,0 +1,33 @@ +/* + Keypress version 2.1.5 (c) 2018 David Mauro. + Licensed under the Apache License, Version 2.0 + http://www.apache.org/licenses/LICENSE-2.0 +*/ +(function(){var o,u,x,y,z,r,v,A,E,B,F,G,q,s,p,k,t,C,H,D={}.hasOwnProperty,j=[].indexOf||function(a){for(var c=0,b=this.length;cj.call(a,"cmd")?!1:!0};g.prototype._prevent_default=function(a,c){if((c||this.should_suppress_event_defaults)&&!this.should_force_event_defaults)if(a.preventDefault?a.preventDefault(): +a.returnValue=!1,a.stopPropagation)return a.stopPropagation()};g.prototype._get_active_combos=function(a){var c,b;c=[];b=v(this._keys_down,function(b){return b!==a});b.push(a);this._match_combo_arrays(b,function(a){return function(b){if(a._cmd_bug_check(b.keys))return c.push(b)}}(this));this._fuzzy_match_combo_arrays(b,function(a){return function(b){if(!(0<=j.call(c,b))&&!b.is_solitary&&a._cmd_bug_check(b.keys))return c.push(b)}}(this));return c};g.prototype._get_potential_combos=function(a){var c, +b,d,e,f;b=[];f=this._registered_combos;d=0;for(e=f.length;dl;e=0<=l?++i:--i)if((c=this._active_combos[e])&&c.is_exclusive&&a.is_exclusive){c=c.keys;if(!h){g=0;for(n=c.length;gj.call(a.keys,b)){h=!1;break}}if(f&&!h){m=a.keys;g=0;for(n=m.length;gj.call(c,b)){f=!0;break}}h&&(d?(c=this._active_combos.splice(e,1)[0],null!=c&&c.reset()):(c=this._active_combos.splice(e,1,a)[0],null!=c&&c.reset(),d=!0),f=!1)}}f&&this._active_combos.unshift(a);return h||f};g.prototype._remove_from_active_combos=function(a){var c,b,d,e;b=d=0;for(e=this._active_combos.length;0<=e?de;b=0<=e?++d:--d)if(c=this._active_combos[b],c===a){a=this._active_combos.splice(b, +1)[0];a.reset();break}};g.prototype._get_possible_sequences=function(){var a,c,b,d,e,f,h,i,g,n,l,m;d=[];n=this._registered_combos;f=0;for(g=n.length;f=l;c=1<=l?++h:--h)if(e=this._sequence.slice(-c),a.is_sequence){if(0>j.call(a.keys,"shift")&&(e=v(e,function(a){return"shift"!==a}),!e.length))continue;c=i=0;for(m=e.length;0<=m?im;c=0<=m?++i:--i)if(a.keys[c]===e[c])b=!0;else{b=!1;break}b&&d.push(a)}}return d};g.prototype._add_key_to_sequence= +function(a,c){var b,d,e,f;this._sequence.push(a);d=this._get_possible_sequences();if(d.length){e=0;for(f=d.length;e=m;b=1<=m?++i:--i)if(f=v(this._sequence,function(a){return 0<=j.call(c.keys,"shift")?!0:"shift"!==a}).slice(-b),c.keys.length===f.length){b=g=0;for(k=f.length;0<=k?gk;b=0<=k?++g:--g)if(e=f[b],!(0>j.call(c.keys,"shift")&&"shift"===e)&&!("shift"===a&&0>j.call(c.keys,"shift")))if(c.keys[b]===e)d=!0;else{d=!1;break}}if(d)return c.is_exclusive&&(this._sequence=[]),c}return!1};g.prototype._receive_input=function(a,c){var b,d;if(this._prevent_capture)this._keys_down.length&& +(this._keys_down=[]);else if(b=y(null!=(d=a.keyCode)?d:a.key),(c||this._keys_down.length||!("alt"===b||b===k))&&b)return c?this._key_down(b,a):this._key_up(b,a)};g.prototype._fire=function(a,c,b,d){"function"===typeof c["on_"+a]&&this._prevent_default(b,!0!==c["on_"+a].call(c["this"],b,c.count,d));"release"===a&&(c.count=0);if("keyup"===a)return c.keyup_fired=!0};g.prototype._match_combo_arrays=function(a,c){var b,d,e,f,h;h=this._registered_combos;e=0;for(f=h.length;ef;b=0<=f?++d:--d)this._keys_down[b]===e&&this._keys_down.splice(b,1)}d=this._get_active_combos(a);e=this._get_potential_combos(a);f=0;for(h=d.length;fj.call(this._keys_down,a)&&this._keys_down.push(a)};g.prototype._handle_combo_down=function(a,c,b,d){var e,f,h,g,k;if(0>j.call(a.keys,b))return!1;this._prevent_default(d,a&&a.prevent_default);e=!1;if(0<=j.call(this._keys_down,b)&&(e=!0,!a.allows_key_repeat()))return!1;h=this._add_to_active_combos(a,b);b=a.keyup_fired=!1;if(a.is_exclusive){g=0;for(k=c.length;g +a.keys.length){b=!0;break}}if(!b&&(a.is_counting&&"function"===typeof a.on_keydown&&(a.count+=1),h))return this._fire("keydown",a,d,e)};g.prototype._key_up=function(a,c){var b,d,e,f,h,g;b=a;(e=z(a,c))&&(a=e);e=s[b];c.shiftKey?e&&0<=j.call(this._keys_down,e)||(a=b):b&&0<=j.call(this._keys_down,b)||(a=e);(f=this._get_sequence(a))&&this._fire("keyup",f,c);if(0>j.call(this._keys_down,a))return!1;f=h=0;for(g=this._keys_down.length;0<=g?hg;f=0<=g?++h:--h)if((d=this._keys_down[f])===a||d===e||d===b){this._keys_down.splice(f, +1);break}d=this._active_combos.length;e=[];g=this._active_combos;f=0;for(h=g.length;fd;b=0<=d?++c:--c)if(a===i._registered_combos[b]){i._registered_combos.splice(b,1);break}else e.push(void 0);return e};if(a instanceof w)return b(a);if("string"===typeof a){a=a.split(" ");c=d=0;for(e=a.length;0<=e?de;c=0<=e?++d:--d)"meta"===a[c]&&(a[c]=k)}f=this._registered_combos;g=[];d=0;for(e=f.length;de;b=0<=e?++d:--d)if(a[b]!==c[b])return!1;return!0};E=function(a,c){var b,d,e;d=0;for(e=a.length;dj.call(c,b))return!1;return!0};A=Array.prototype.indexOf||function(a,c){var b,d,e;b=d=0; +for(e=a.length;0<=e?d<=e:d>=e;b=0<=e?++d:--d)if(a[b]===c)return b;return-1};B=function(a,c){var b,d,e,f;e=d=0;for(f=a.length;e=d)d=b;else return!1;return!0};p=function(){if(o.debug)return console.log.apply(console,arguments)};F=function(a){var c,b,d;c=!1;for(d in q)if(b=q[d],a===b){c=!0;break}if(!c)for(d in s)if(b=s[d],a===b){c=!0;break}return c};H=function(a){var c,b,d,e,f,g,i;f=!0;a.keys.length||p("You're trying to bind a combo with no keys:",a);b=g=0;for(i=a.keys.length;0<= +i?gi;b=0<=i?++g:--g)d=a.keys[b],(c=G[d])&&(d=a.keys[b]=c),"meta"===d&&a.keys.splice(b,1,k),"cmd"===d&&p('Warning: use the "meta" key rather than "cmd" for Windows compatibility');i=a.keys;c=0;for(g=i.length;c",",":"<","'":'"', +";":":","[":"{","]":"}","\\":"|","`":"~","=":"+","-":"_",1:"!",2:"@",3:"#",4:"$",5:"%",6:"^",7:"&",8:"*",9:"(","0":")"};q={"0":"\\",8:"backspace",9:"tab",12:"num",13:"enter",16:"shift",17:"ctrl",18:"alt",19:"pause",20:"caps",27:"esc",32:"space",33:"pageup",34:"pagedown",35:"end",36:"home",37:"left",38:"up",39:"right",40:"down",44:"print",45:"insert",46:"delete",48:"0",49:"1",50:"2",51:"3",52:"4",53:"5",54:"6",55:"7",56:"8",57:"9",65:"a",66:"b",67:"c",68:"d",69:"e",70:"f",71:"g",72:"h",73:"i",74:"j", +75:"k",76:"l",77:"m",78:"n",79:"o",80:"p",81:"q",82:"r",83:"s",84:"t",85:"u",86:"v",87:"w",88:"x",89:"y",90:"z",91:"cmd",92:"cmd",93:"cmd",96:"num_0",97:"num_1",98:"num_2",99:"num_3",100:"num_4",101:"num_5",102:"num_6",103:"num_7",104:"num_8",105:"num_9",106:"num_multiply",107:"num_add",108:"num_enter",109:"num_subtract",110:"num_decimal",111:"num_divide",112:"f1",113:"f2",114:"f3",115:"f4",116:"f5",117:"f6",118:"f7",119:"f8",120:"f9",121:"f10",122:"f11",123:"f12",124:"print",144:"num",145:"scroll", +186:";",187:"=",188:",",189:"-",190:".",191:"/",192:"`",219:"[",220:"\\",221:"]",222:"'",223:"`",224:"cmd",225:"alt",57392:"ctrl",63289:"num",59:";",61:"=",173:"-"};o._keycode_dictionary=q;o._is_array_in_array_sorted=B;-1!==navigator.userAgent.indexOf("Mac OS X")&&(k="cmd");-1!==navigator.userAgent.indexOf("Opera")&&(q["17"]="cmd");"function"===typeof define&&define.amd?define([],function(){return o}):"undefined"!==typeof exports&&null!==exports?exports.keypress=o:window.keypress=o}).call(this); diff --git a/keypress.coffee b/keypress.coffee index e525735..d84fe40 100644 --- a/keypress.coffee +++ b/keypress.coffee @@ -16,7 +16,7 @@ limitations under the License. Keypress is a robust keyboard input capturing Javascript utility focused on input for games. -version 2.1.3 +version 2.1.5 ### ### diff --git a/keypress.js b/keypress.js index 21b70c4..16fe270 100644 --- a/keypress.js +++ b/keypress.js @@ -18,9 +18,10 @@ limitations under the License. Keypress is a robust keyboard input capturing Javascript utility focused on input for games. -version 2.1.3 +version 2.1.5 */ + /* Combo options available and their defaults: keys : [] - An array of the keys pressed together to activate combo. @@ -38,1113 +39,1117 @@ Combo options available and their defaults: on_release : null - A function that is called when all keys in the combo are released. this : undefined - Defines the scope for your callback functions. */ -var Combo, keypress, _change_keycodes_by_browser, _compare_arrays, _compare_arrays_sorted, _convert_key_to_readable, _convert_to_shifted_key, _decide_meta_key, _factory_defaults, _filter_array, _index_of_in_array, _is_array_in_array, _is_array_in_array_sorted, _key_is_valid, _keycode_alternate_names, _keycode_dictionary, _keycode_shifted_keys, _log_error, _metakey, _modifier_event_mapping, _modifier_keys, _validate_combo, - __hasProp = {}.hasOwnProperty, - __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; -_factory_defaults = { - is_unordered: false, - is_counting: false, - is_exclusive: false, - is_solitary: false, - prevent_default: false, - prevent_repeat: false, - normalize_caps_lock: false -}; +(function() { + var Combo, keypress, _change_keycodes_by_browser, _compare_arrays, _compare_arrays_sorted, _convert_key_to_readable, _convert_to_shifted_key, _decide_meta_key, _factory_defaults, _filter_array, _index_of_in_array, _is_array_in_array, _is_array_in_array_sorted, _key_is_valid, _keycode_alternate_names, _keycode_dictionary, _keycode_shifted_keys, _log_error, _metakey, _modifier_event_mapping, _modifier_keys, _validate_combo, + __hasProp = {}.hasOwnProperty, + __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; + + _factory_defaults = { + is_unordered: false, + is_counting: false, + is_exclusive: false, + is_solitary: false, + prevent_default: false, + prevent_repeat: false, + normalize_caps_lock: false + }; -_modifier_keys = ["meta", "alt", "option", "ctrl", "shift", "cmd"]; + _modifier_keys = ["meta", "alt", "option", "ctrl", "shift", "cmd"]; -_metakey = "ctrl"; + _metakey = "ctrl"; -keypress = {}; + keypress = {}; -keypress.debug = false; + keypress.debug = false; -Combo = (function() { - function Combo(dictionary) { - var property, value; - for (property in dictionary) { - if (!__hasProp.call(dictionary, property)) continue; - value = dictionary[property]; - if (value !== false) { - this[property] = value; + Combo = (function() { + function Combo(dictionary) { + var property, value; + for (property in dictionary) { + if (!__hasProp.call(dictionary, property)) continue; + value = dictionary[property]; + if (value !== false) { + this[property] = value; + } } + this.keys = this.keys || []; + this.count = this.count || 0; } - this.keys = this.keys || []; - this.count = this.count || 0; - } - Combo.prototype.allows_key_repeat = function() { - return !this.prevent_repeat && typeof this.on_keydown === "function"; - }; + Combo.prototype.allows_key_repeat = function() { + return !this.prevent_repeat && typeof this.on_keydown === "function"; + }; - Combo.prototype.reset = function() { - this.count = 0; - return this.keyup_fired = null; - }; + Combo.prototype.reset = function() { + this.count = 0; + return this.keyup_fired = null; + }; - return Combo; + return Combo; -})(); + })(); -keypress.Listener = (function() { - function Listener(element, defaults) { - var attach_handler, property, value; - if ((typeof jQuery !== "undefined" && jQuery !== null) && element instanceof jQuery) { - if (element.length !== 1) { - _log_error("Warning: your jQuery selector should have exactly one object."); + keypress.Listener = (function() { + function Listener(element, defaults) { + var attach_handler, property, value; + if ((typeof jQuery !== "undefined" && jQuery !== null) && element instanceof jQuery) { + if (element.length !== 1) { + _log_error("Warning: your jQuery selector should have exactly one object."); + } + element = element[0]; } - element = element[0]; - } - this.should_suppress_event_defaults = false; - this.should_force_event_defaults = false; - this.sequence_delay = 800; - this._registered_combos = []; - this._keys_down = []; - this._active_combos = []; - this._sequence = []; - this._sequence_timer = null; - this._prevent_capture = false; - this._defaults = defaults || {}; - for (property in _factory_defaults) { - if (!__hasProp.call(_factory_defaults, property)) continue; - value = _factory_defaults[property]; - this._defaults[property] = this._defaults[property] || value; - } - this.element = element || document.body; - attach_handler = function(target, event, handler) { - if (target.addEventListener) { - target.addEventListener(event, handler); - } else if (target.attachEvent) { - target.attachEvent("on" + event, handler); - } - return handler; - }; - this.keydown_event = attach_handler(this.element, "keydown", (function(_this) { - return function(e) { - e = e || window.event; - _this._receive_input(e, true); - return _this._bug_catcher(e); - }; - })(this)); - this.keyup_event = attach_handler(this.element, "keyup", (function(_this) { - return function(e) { - e = e || window.event; - return _this._receive_input(e, false); + this.should_suppress_event_defaults = false; + this.should_force_event_defaults = false; + this.sequence_delay = 800; + this._registered_combos = []; + this._keys_down = []; + this._active_combos = []; + this._sequence = []; + this._sequence_timer = null; + this._prevent_capture = false; + this._defaults = defaults || {}; + for (property in _factory_defaults) { + if (!__hasProp.call(_factory_defaults, property)) continue; + value = _factory_defaults[property]; + this._defaults[property] = this._defaults[property] || value; + } + this.element = element || document.body; + attach_handler = function(target, event, handler) { + if (target.addEventListener) { + target.addEventListener(event, handler); + } else if (target.attachEvent) { + target.attachEvent("on" + event, handler); + } + return handler; }; - })(this)); - this.blur_event = attach_handler(window, "blur", (function(_this) { - return function() { - var key, _i, _len, _ref; - _ref = _this._keys_down; - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - key = _ref[_i]; - _this._key_up(key, {}); + this.keydown_event = attach_handler(this.element, "keydown", (function(_this) { + return function(e) { + e = e || window.event; + _this._receive_input(e, true); + return _this._bug_catcher(e); + }; + })(this)); + this.keyup_event = attach_handler(this.element, "keyup", (function(_this) { + return function(e) { + e = e || window.event; + return _this._receive_input(e, false); + }; + })(this)); + this.blur_event = attach_handler(window, "blur", (function(_this) { + return function() { + var key, _i, _len, _ref; + _ref = _this._keys_down; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + key = _ref[_i]; + _this._key_up(key, {}); + } + return _this._keys_down = []; + }; + })(this)); + } + + Listener.prototype.destroy = function() { + var remove_handler; + remove_handler = function(target, event, handler) { + if (target.removeEventListener != null) { + return target.removeEventListener(event, handler); + } else if (target.removeEvent != null) { + return target.removeEvent("on" + event, handler); } - return _this._keys_down = []; }; - })(this)); - } + remove_handler(this.element, "keydown", this.keydown_event); + remove_handler(this.element, "keyup", this.keyup_event); + return remove_handler(window, "blur", this.blur_event); + }; - Listener.prototype.destroy = function() { - var remove_handler; - remove_handler = function(target, event, handler) { - if (target.removeEventListener != null) { - return target.removeEventListener(event, handler); - } else if (target.removeEvent != null) { - return target.removeEvent("on" + event, handler); + Listener.prototype._bug_catcher = function(e) { + var _ref, _ref1; + if (_metakey === "cmd" && __indexOf.call(this._keys_down, "cmd") >= 0 && ((_ref = _convert_key_to_readable((_ref1 = e.keyCode) != null ? _ref1 : e.key)) !== "cmd" && _ref !== "shift" && _ref !== "alt" && _ref !== "caps" && _ref !== "tab")) { + return this._receive_input(e, false); } }; - remove_handler(this.element, "keydown", this.keydown_event); - remove_handler(this.element, "keyup", this.keyup_event); - return remove_handler(window, "blur", this.blur_event); - }; - Listener.prototype._bug_catcher = function(e) { - var _ref, _ref1; - if (_metakey === "cmd" && __indexOf.call(this._keys_down, "cmd") >= 0 && ((_ref = _convert_key_to_readable((_ref1 = e.keyCode) != null ? _ref1 : e.key)) !== "cmd" && _ref !== "shift" && _ref !== "alt" && _ref !== "caps" && _ref !== "tab")) { - return this._receive_input(e, false); - } - }; - - Listener.prototype._cmd_bug_check = function(combo_keys) { - if (_metakey === "cmd" && __indexOf.call(this._keys_down, "cmd") >= 0 && __indexOf.call(combo_keys, "cmd") < 0) { - return false; - } - return true; - }; - - Listener.prototype._prevent_default = function(e, should_prevent) { - if ((should_prevent || this.should_suppress_event_defaults) && !this.should_force_event_defaults) { - if (e.preventDefault) { - e.preventDefault(); - } else { - e.returnValue = false; - } - if (e.stopPropagation) { - return e.stopPropagation(); + Listener.prototype._cmd_bug_check = function(combo_keys) { + if (_metakey === "cmd" && __indexOf.call(this._keys_down, "cmd") >= 0 && __indexOf.call(combo_keys, "cmd") < 0) { + return false; } - } - }; + return true; + }; - Listener.prototype._get_active_combos = function(key) { - var active_combos, keys_down; - active_combos = []; - keys_down = _filter_array(this._keys_down, function(down_key) { - return down_key !== key; - }); - keys_down.push(key); - this._match_combo_arrays(keys_down, (function(_this) { - return function(match) { - if (_this._cmd_bug_check(match.keys)) { - return active_combos.push(match); + Listener.prototype._prevent_default = function(e, should_prevent) { + if ((should_prevent || this.should_suppress_event_defaults) && !this.should_force_event_defaults) { + if (e.preventDefault) { + e.preventDefault(); + } else { + e.returnValue = false; } - }; - })(this)); - this._fuzzy_match_combo_arrays(keys_down, (function(_this) { - return function(match) { - if (__indexOf.call(active_combos, match) >= 0) { - return; + if (e.stopPropagation) { + return e.stopPropagation(); } - if (!(match.is_solitary || !_this._cmd_bug_check(match.keys))) { - return active_combos.push(match); + } + }; + + Listener.prototype._get_active_combos = function(key) { + var active_combos, keys_down; + active_combos = []; + keys_down = _filter_array(this._keys_down, function(down_key) { + return down_key !== key; + }); + keys_down.push(key); + this._match_combo_arrays(keys_down, (function(_this) { + return function(match) { + if (_this._cmd_bug_check(match.keys)) { + return active_combos.push(match); + } + }; + })(this)); + this._fuzzy_match_combo_arrays(keys_down, (function(_this) { + return function(match) { + if (__indexOf.call(active_combos, match) >= 0) { + return; + } + if (!(match.is_solitary || !_this._cmd_bug_check(match.keys))) { + return active_combos.push(match); + } + }; + })(this)); + return active_combos; + }; + + Listener.prototype._get_potential_combos = function(key) { + var combo, potentials, _i, _len, _ref; + potentials = []; + _ref = this._registered_combos; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + combo = _ref[_i]; + if (combo.is_sequence) { + continue; } - }; - })(this)); - return active_combos; - }; + if (__indexOf.call(combo.keys, key) >= 0 && this._cmd_bug_check(combo.keys)) { + potentials.push(combo); + } + } + return potentials; + }; - Listener.prototype._get_potential_combos = function(key) { - var combo, potentials, _i, _len, _ref; - potentials = []; - _ref = this._registered_combos; - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - combo = _ref[_i]; - if (combo.is_sequence) { - continue; + Listener.prototype._add_to_active_combos = function(combo) { + var active_combo, active_key, active_keys, already_replaced, combo_key, i, should_prepend, should_replace, _i, _j, _k, _len, _len1, _ref, _ref1; + should_replace = false; + should_prepend = true; + already_replaced = false; + if (__indexOf.call(this._active_combos, combo) >= 0) { + return true; + } else if (this._active_combos.length) { + for (i = _i = 0, _ref = this._active_combos.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) { + active_combo = this._active_combos[i]; + if (!(active_combo && active_combo.is_exclusive && combo.is_exclusive)) { + continue; + } + active_keys = active_combo.keys; + if (!should_replace) { + for (_j = 0, _len = active_keys.length; _j < _len; _j++) { + active_key = active_keys[_j]; + should_replace = true; + if (__indexOf.call(combo.keys, active_key) < 0) { + should_replace = false; + break; + } + } + } + if (should_prepend && !should_replace) { + _ref1 = combo.keys; + for (_k = 0, _len1 = _ref1.length; _k < _len1; _k++) { + combo_key = _ref1[_k]; + should_prepend = false; + if (__indexOf.call(active_keys, combo_key) < 0) { + should_prepend = true; + break; + } + } + } + if (should_replace) { + if (already_replaced) { + active_combo = this._active_combos.splice(i, 1)[0]; + if (active_combo != null) { + active_combo.reset(); + } + } else { + active_combo = this._active_combos.splice(i, 1, combo)[0]; + if (active_combo != null) { + active_combo.reset(); + } + already_replaced = true; + } + should_prepend = false; + } + } } - if (__indexOf.call(combo.keys, key) >= 0 && this._cmd_bug_check(combo.keys)) { - potentials.push(combo); + if (should_prepend) { + this._active_combos.unshift(combo); } - } - return potentials; - }; + return should_replace || should_prepend; + }; - Listener.prototype._add_to_active_combos = function(combo) { - var active_combo, active_key, active_keys, already_replaced, combo_key, i, should_prepend, should_replace, _i, _j, _k, _len, _len1, _ref, _ref1; - should_replace = false; - should_prepend = true; - already_replaced = false; - if (__indexOf.call(this._active_combos, combo) >= 0) { - return true; - } else if (this._active_combos.length) { + Listener.prototype._remove_from_active_combos = function(combo) { + var active_combo, i, _i, _ref; for (i = _i = 0, _ref = this._active_combos.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) { active_combo = this._active_combos[i]; - if (!(active_combo && active_combo.is_exclusive && combo.is_exclusive)) { - continue; + if (active_combo === combo) { + combo = this._active_combos.splice(i, 1)[0]; + combo.reset(); + break; } - active_keys = active_combo.keys; - if (!should_replace) { - for (_j = 0, _len = active_keys.length; _j < _len; _j++) { - active_key = active_keys[_j]; - should_replace = true; - if (__indexOf.call(combo.keys, active_key) < 0) { - should_replace = false; - break; + } + }; + + Listener.prototype._get_possible_sequences = function() { + var combo, i, j, match, matches, sequence, _i, _j, _k, _len, _ref, _ref1, _ref2; + matches = []; + _ref = this._registered_combos; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + combo = _ref[_i]; + for (j = _j = 1, _ref1 = this._sequence.length; 1 <= _ref1 ? _j <= _ref1 : _j >= _ref1; j = 1 <= _ref1 ? ++_j : --_j) { + sequence = this._sequence.slice(-j); + if (!combo.is_sequence) { + continue; + } + if (__indexOf.call(combo.keys, "shift") < 0) { + sequence = _filter_array(sequence, function(key) { + return key !== "shift"; + }); + if (!sequence.length) { + continue; } } - } - if (should_prepend && !should_replace) { - _ref1 = combo.keys; - for (_k = 0, _len1 = _ref1.length; _k < _len1; _k++) { - combo_key = _ref1[_k]; - should_prepend = false; - if (__indexOf.call(active_keys, combo_key) < 0) { - should_prepend = true; + for (i = _k = 0, _ref2 = sequence.length; 0 <= _ref2 ? _k < _ref2 : _k > _ref2; i = 0 <= _ref2 ? ++_k : --_k) { + if (combo.keys[i] === sequence[i]) { + match = true; + } else { + match = false; break; } } - } - if (should_replace) { - if (already_replaced) { - active_combo = this._active_combos.splice(i, 1)[0]; - if (active_combo != null) { - active_combo.reset(); - } - } else { - active_combo = this._active_combos.splice(i, 1, combo)[0]; - if (active_combo != null) { - active_combo.reset(); - } - already_replaced = true; + if (match) { + matches.push(combo); } - should_prepend = false; } } - } - if (should_prepend) { - this._active_combos.unshift(combo); - } - return should_replace || should_prepend; - }; + return matches; + }; - Listener.prototype._remove_from_active_combos = function(combo) { - var active_combo, i, _i, _ref; - for (i = _i = 0, _ref = this._active_combos.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) { - active_combo = this._active_combos[i]; - if (active_combo === combo) { - combo = this._active_combos.splice(i, 1)[0]; - combo.reset(); - break; + Listener.prototype._add_key_to_sequence = function(key, e) { + var combo, sequence_combos, _i, _len; + this._sequence.push(key); + sequence_combos = this._get_possible_sequences(); + if (sequence_combos.length) { + for (_i = 0, _len = sequence_combos.length; _i < _len; _i++) { + combo = sequence_combos[_i]; + this._prevent_default(e, combo.prevent_default); + } + if (this._sequence_timer) { + clearTimeout(this._sequence_timer); + } + if (this.sequence_delay > -1) { + this._sequence_timer = setTimeout((function(_this) { + return function() { + return _this._sequence = []; + }; + })(this), this.sequence_delay); + } + } else { + this._sequence = []; } - } - }; + }; - Listener.prototype._get_possible_sequences = function() { - var combo, i, j, match, matches, sequence, _i, _j, _k, _len, _ref, _ref1, _ref2; - matches = []; - _ref = this._registered_combos; - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - combo = _ref[_i]; - for (j = _j = 1, _ref1 = this._sequence.length; 1 <= _ref1 ? _j <= _ref1 : _j >= _ref1; j = 1 <= _ref1 ? ++_j : --_j) { - sequence = this._sequence.slice(-j); + Listener.prototype._get_sequence = function(key) { + var combo, i, j, match, seq_key, sequence, _i, _j, _k, _len, _ref, _ref1, _ref2; + _ref = this._registered_combos; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + combo = _ref[_i]; if (!combo.is_sequence) { continue; } - if (__indexOf.call(combo.keys, "shift") < 0) { - sequence = _filter_array(sequence, function(key) { - return key !== "shift"; - }); - if (!sequence.length) { + for (j = _j = 1, _ref1 = this._sequence.length; 1 <= _ref1 ? _j <= _ref1 : _j >= _ref1; j = 1 <= _ref1 ? ++_j : --_j) { + sequence = (_filter_array(this._sequence, function(seq_key) { + if (__indexOf.call(combo.keys, "shift") >= 0) { + return true; + } + return seq_key !== "shift"; + })).slice(-j); + if (combo.keys.length !== sequence.length) { continue; } - } - for (i = _k = 0, _ref2 = sequence.length; 0 <= _ref2 ? _k < _ref2 : _k > _ref2; i = 0 <= _ref2 ? ++_k : --_k) { - if (combo.keys[i] === sequence[i]) { - match = true; - } else { - match = false; - break; + for (i = _k = 0, _ref2 = sequence.length; 0 <= _ref2 ? _k < _ref2 : _k > _ref2; i = 0 <= _ref2 ? ++_k : --_k) { + seq_key = sequence[i]; + if (__indexOf.call(combo.keys, "shift") < 0) { + if (seq_key === "shift") { + continue; + } + } + if (key === "shift" && __indexOf.call(combo.keys, "shift") < 0) { + continue; + } + if (combo.keys[i] === seq_key) { + match = true; + } else { + match = false; + break; + } } } if (match) { - matches.push(combo); + if (combo.is_exclusive) { + this._sequence = []; + } + return combo; } } - } - return matches; - }; + return false; + }; - Listener.prototype._add_key_to_sequence = function(key, e) { - var combo, sequence_combos, _i, _len; - this._sequence.push(key); - sequence_combos = this._get_possible_sequences(); - if (sequence_combos.length) { - for (_i = 0, _len = sequence_combos.length; _i < _len; _i++) { - combo = sequence_combos[_i]; - this._prevent_default(e, combo.prevent_default); - } - if (this._sequence_timer) { - clearTimeout(this._sequence_timer); - } - if (this.sequence_delay > -1) { - this._sequence_timer = setTimeout((function(_this) { - return function() { - return _this._sequence = []; - }; - })(this), this.sequence_delay); + Listener.prototype._receive_input = function(e, is_keydown) { + var key, _ref; + if (this._prevent_capture) { + if (this._keys_down.length) { + this._keys_down = []; + } + return; } - } else { - this._sequence = []; - } - }; - - Listener.prototype._get_sequence = function(key) { - var combo, i, j, match, seq_key, sequence, _i, _j, _k, _len, _ref, _ref1, _ref2; - _ref = this._registered_combos; - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - combo = _ref[_i]; - if (!combo.is_sequence) { - continue; + key = _convert_key_to_readable((_ref = e.keyCode) != null ? _ref : e.key); + if (!is_keydown && !this._keys_down.length && (key === "alt" || key === _metakey)) { + return; } - for (j = _j = 1, _ref1 = this._sequence.length; 1 <= _ref1 ? _j <= _ref1 : _j >= _ref1; j = 1 <= _ref1 ? ++_j : --_j) { - sequence = (_filter_array(this._sequence, function(seq_key) { - if (__indexOf.call(combo.keys, "shift") >= 0) { - return true; - } - return seq_key !== "shift"; - })).slice(-j); - if (combo.keys.length !== sequence.length) { - continue; - } - for (i = _k = 0, _ref2 = sequence.length; 0 <= _ref2 ? _k < _ref2 : _k > _ref2; i = 0 <= _ref2 ? ++_k : --_k) { - seq_key = sequence[i]; - if (__indexOf.call(combo.keys, "shift") < 0) { - if (seq_key === "shift") { - continue; - } - } - if (key === "shift" && __indexOf.call(combo.keys, "shift") < 0) { - continue; - } - if (combo.keys[i] === seq_key) { - match = true; - } else { - match = false; - break; - } - } + if (!key) { + return; } - if (match) { - if (combo.is_exclusive) { - this._sequence = []; - } - return combo; + if (is_keydown) { + return this._key_down(key, e); + } else { + return this._key_up(key, e); } - } - return false; - }; + }; - Listener.prototype._receive_input = function(e, is_keydown) { - var key, _ref; - if (this._prevent_capture) { - if (this._keys_down.length) { - this._keys_down = []; + Listener.prototype._fire = function(event, combo, key_event, is_autorepeat) { + if (typeof combo["on_" + event] === "function") { + this._prevent_default(key_event, combo["on_" + event].call(combo["this"], key_event, combo.count, is_autorepeat) !== true); } - return; - } - key = _convert_key_to_readable((_ref = e.keyCode) != null ? _ref : e.key); - if (!is_keydown && !this._keys_down.length && (key === "alt" || key === _metakey)) { - return; - } - if (!key) { - return; - } - if (is_keydown) { - return this._key_down(key, e); - } else { - return this._key_up(key, e); - } - }; - - Listener.prototype._fire = function(event, combo, key_event, is_autorepeat) { - if (typeof combo["on_" + event] === "function") { - this._prevent_default(key_event, combo["on_" + event].call(combo["this"], key_event, combo.count, is_autorepeat) !== true); - } - if (event === "release") { - combo.count = 0; - } - if (event === "keyup") { - return combo.keyup_fired = true; - } - }; - - Listener.prototype._match_combo_arrays = function(potential_match, match_handler) { - var combo_potential_match, source_combo, _i, _len, _ref; - _ref = this._registered_combos; - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - source_combo = _ref[_i]; - combo_potential_match = potential_match.slice(0); - if (source_combo.normalize_caps_lock && __indexOf.call(combo_potential_match, "caps") >= 0) { - combo_potential_match.splice(combo_potential_match.indexOf("caps"), 1); + if (event === "release") { + combo.count = 0; } - if ((!source_combo.is_unordered && _compare_arrays_sorted(combo_potential_match, source_combo.keys)) || (source_combo.is_unordered && _compare_arrays(combo_potential_match, source_combo.keys))) { - match_handler(source_combo); + if (event === "keyup") { + return combo.keyup_fired = true; } - } - }; + }; + + Listener.prototype._match_combo_arrays = function(potential_match, match_handler) { + var combo_potential_match, source_combo, _i, _len, _ref; + _ref = this._registered_combos; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + source_combo = _ref[_i]; + combo_potential_match = potential_match.slice(0); + if (source_combo.normalize_caps_lock && __indexOf.call(combo_potential_match, "caps") >= 0) { + combo_potential_match.splice(combo_potential_match.indexOf("caps"), 1); + } + if ((!source_combo.is_unordered && _compare_arrays_sorted(combo_potential_match, source_combo.keys)) || (source_combo.is_unordered && _compare_arrays(combo_potential_match, source_combo.keys))) { + match_handler(source_combo); + } + } + }; - Listener.prototype._fuzzy_match_combo_arrays = function(potential_match, match_handler) { - var source_combo, _i, _len, _ref; - _ref = this._registered_combos; - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - source_combo = _ref[_i]; - if ((!source_combo.is_unordered && _is_array_in_array_sorted(source_combo.keys, potential_match)) || (source_combo.is_unordered && _is_array_in_array(source_combo.keys, potential_match))) { - match_handler(source_combo); + Listener.prototype._fuzzy_match_combo_arrays = function(potential_match, match_handler) { + var source_combo, _i, _len, _ref; + _ref = this._registered_combos; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + source_combo = _ref[_i]; + if ((!source_combo.is_unordered && _is_array_in_array_sorted(source_combo.keys, potential_match)) || (source_combo.is_unordered && _is_array_in_array(source_combo.keys, potential_match))) { + match_handler(source_combo); + } } - } - }; + }; - Listener.prototype._keys_remain = function(combo) { - var key, keys_remain, _i, _len, _ref; - _ref = combo.keys; - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - key = _ref[_i]; - if (__indexOf.call(this._keys_down, key) >= 0) { - keys_remain = true; - break; + Listener.prototype._keys_remain = function(combo) { + var key, keys_remain, _i, _len, _ref; + _ref = combo.keys; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + key = _ref[_i]; + if (__indexOf.call(this._keys_down, key) >= 0) { + keys_remain = true; + break; + } } - } - return keys_remain; - }; + return keys_remain; + }; - Listener.prototype._key_down = function(key, e) { - var combo, combos, event_mod, i, mod, potential, potential_combos, sequence_combo, shifted_key, _i, _j, _k, _len, _len1, _ref; - shifted_key = _convert_to_shifted_key(key, e); - if (shifted_key) { - key = shifted_key; - } - this._add_key_to_sequence(key, e); - sequence_combo = this._get_sequence(key); - if (sequence_combo) { - this._fire("keydown", sequence_combo, e); - } - for (mod in _modifier_event_mapping) { - event_mod = _modifier_event_mapping[mod]; - if (!e[event_mod]) { - continue; + Listener.prototype._key_down = function(key, e) { + var combo, combos, event_mod, i, mod, potential, potential_combos, sequence_combo, shifted_key, _i, _j, _k, _len, _len1, _ref; + shifted_key = _convert_to_shifted_key(key, e); + if (shifted_key) { + key = shifted_key; } - if (mod === key || __indexOf.call(this._keys_down, mod) >= 0) { - continue; + this._add_key_to_sequence(key, e); + sequence_combo = this._get_sequence(key); + if (sequence_combo) { + this._fire("keydown", sequence_combo, e); } - this._keys_down.push(mod); - } - for (mod in _modifier_event_mapping) { - event_mod = _modifier_event_mapping[mod]; - if (mod === key) { - continue; + for (mod in _modifier_event_mapping) { + event_mod = _modifier_event_mapping[mod]; + if (!e[event_mod]) { + continue; + } + if (mod === key || __indexOf.call(this._keys_down, mod) >= 0) { + continue; + } + this._keys_down.push(mod); } - if (__indexOf.call(this._keys_down, mod) >= 0 && !e[event_mod]) { - if (mod === "cmd" && _metakey !== "cmd") { + for (mod in _modifier_event_mapping) { + event_mod = _modifier_event_mapping[mod]; + if (mod === key) { continue; } - for (i = _i = 0, _ref = this._keys_down.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) { - if (this._keys_down[i] === mod) { - this._keys_down.splice(i, 1); + if (__indexOf.call(this._keys_down, mod) >= 0 && !e[event_mod]) { + if (mod === "cmd" && _metakey !== "cmd") { + continue; + } + for (i = _i = 0, _ref = this._keys_down.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) { + if (this._keys_down[i] === mod) { + this._keys_down.splice(i, 1); + } } } } - } - combos = this._get_active_combos(key); - potential_combos = this._get_potential_combos(key); - for (_j = 0, _len = combos.length; _j < _len; _j++) { - combo = combos[_j]; - this._handle_combo_down(combo, potential_combos, key, e); - } - if (potential_combos.length) { - for (_k = 0, _len1 = potential_combos.length; _k < _len1; _k++) { - potential = potential_combos[_k]; - this._prevent_default(e, potential.prevent_default); + combos = this._get_active_combos(key); + potential_combos = this._get_potential_combos(key); + for (_j = 0, _len = combos.length; _j < _len; _j++) { + combo = combos[_j]; + this._handle_combo_down(combo, potential_combos, key, e); } - } - if (__indexOf.call(this._keys_down, key) < 0) { - this._keys_down.push(key); - } - }; + if (potential_combos.length) { + for (_k = 0, _len1 = potential_combos.length; _k < _len1; _k++) { + potential = potential_combos[_k]; + this._prevent_default(e, potential.prevent_default); + } + } + if (__indexOf.call(this._keys_down, key) < 0) { + this._keys_down.push(key); + } + }; - Listener.prototype._handle_combo_down = function(combo, potential_combos, key, e) { - var is_autorepeat, is_other_exclusive, potential_combo, result, _i, _len; - if (__indexOf.call(combo.keys, key) < 0) { - return false; - } - this._prevent_default(e, combo && combo.prevent_default); - is_autorepeat = false; - if (__indexOf.call(this._keys_down, key) >= 0) { - is_autorepeat = true; - if (!combo.allows_key_repeat()) { + Listener.prototype._handle_combo_down = function(combo, potential_combos, key, e) { + var is_autorepeat, is_other_exclusive, potential_combo, result, _i, _len; + if (__indexOf.call(combo.keys, key) < 0) { return false; } - } - result = this._add_to_active_combos(combo, key); - combo.keyup_fired = false; - is_other_exclusive = false; - if (combo.is_exclusive) { - for (_i = 0, _len = potential_combos.length; _i < _len; _i++) { - potential_combo = potential_combos[_i]; - if (potential_combo.is_exclusive && potential_combo.keys.length > combo.keys.length) { - is_other_exclusive = true; - break; + this._prevent_default(e, combo && combo.prevent_default); + is_autorepeat = false; + if (__indexOf.call(this._keys_down, key) >= 0) { + is_autorepeat = true; + if (!combo.allows_key_repeat()) { + return false; } } - } - if (!is_other_exclusive) { - if (combo.is_counting && typeof combo.on_keydown === "function") { - combo.count += 1; + result = this._add_to_active_combos(combo, key); + combo.keyup_fired = false; + is_other_exclusive = false; + if (combo.is_exclusive) { + for (_i = 0, _len = potential_combos.length; _i < _len; _i++) { + potential_combo = potential_combos[_i]; + if (potential_combo.is_exclusive && potential_combo.keys.length > combo.keys.length) { + is_other_exclusive = true; + break; + } + } } - if (result) { - return this._fire("keydown", combo, e, is_autorepeat); + if (!is_other_exclusive) { + if (combo.is_counting && typeof combo.on_keydown === "function") { + combo.count += 1; + } + if (result) { + return this._fire("keydown", combo, e, is_autorepeat); + } } - } - }; + }; - Listener.prototype._key_up = function(key, e) { - var active_combo, active_combos_length, combo, combos, i, sequence_combo, shifted_key, unshifted_key, _i, _j, _k, _l, _len, _len1, _len2, _ref, _ref1, _ref2, _ref3; - unshifted_key = key; - shifted_key = _convert_to_shifted_key(key, e); - if (shifted_key) { - key = shifted_key; - } - shifted_key = _keycode_shifted_keys[unshifted_key]; - if (e.shiftKey) { - if (!(shifted_key && __indexOf.call(this._keys_down, shifted_key) >= 0)) { - key = unshifted_key; - } - } else { - if (!(unshifted_key && __indexOf.call(this._keys_down, unshifted_key) >= 0)) { + Listener.prototype._key_up = function(key, e) { + var active_combo, active_combos_length, combo, combos, i, sequence_combo, shifted_key, unshifted_key, _i, _j, _k, _l, _len, _len1, _len2, _ref, _ref1, _ref2, _ref3; + unshifted_key = key; + shifted_key = _convert_to_shifted_key(key, e); + if (shifted_key) { key = shifted_key; } - } - sequence_combo = this._get_sequence(key); - if (sequence_combo) { - this._fire("keyup", sequence_combo, e); - } - if (__indexOf.call(this._keys_down, key) < 0) { - return false; - } - for (i = _i = 0, _ref = this._keys_down.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) { - if ((_ref1 = this._keys_down[i]) === key || _ref1 === shifted_key || _ref1 === unshifted_key) { - this._keys_down.splice(i, 1); - break; + shifted_key = _keycode_shifted_keys[unshifted_key]; + if (e.shiftKey) { + if (!(shifted_key && __indexOf.call(this._keys_down, shifted_key) >= 0)) { + key = unshifted_key; + } + } else { + if (!(unshifted_key && __indexOf.call(this._keys_down, unshifted_key) >= 0)) { + key = shifted_key; + } } - } - active_combos_length = this._active_combos.length; - combos = []; - _ref2 = this._active_combos; - for (_j = 0, _len = _ref2.length; _j < _len; _j++) { - active_combo = _ref2[_j]; - if (__indexOf.call(active_combo.keys, key) >= 0) { - combos.push(active_combo); + sequence_combo = this._get_sequence(key); + if (sequence_combo) { + this._fire("keyup", sequence_combo, e); } - } - for (_k = 0, _len1 = combos.length; _k < _len1; _k++) { - combo = combos[_k]; - this._handle_combo_up(combo, e, key); - } - if (active_combos_length > 1) { - _ref3 = this._active_combos; - for (_l = 0, _len2 = _ref3.length; _l < _len2; _l++) { - active_combo = _ref3[_l]; - if (active_combo === void 0 || __indexOf.call(combos, active_combo) >= 0) { - continue; + if (__indexOf.call(this._keys_down, key) < 0) { + return false; + } + for (i = _i = 0, _ref = this._keys_down.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) { + if ((_ref1 = this._keys_down[i]) === key || _ref1 === shifted_key || _ref1 === unshifted_key) { + this._keys_down.splice(i, 1); + break; } - if (!this._keys_remain(active_combo)) { - this._remove_from_active_combos(active_combo); + } + active_combos_length = this._active_combos.length; + combos = []; + _ref2 = this._active_combos; + for (_j = 0, _len = _ref2.length; _j < _len; _j++) { + active_combo = _ref2[_j]; + if (__indexOf.call(active_combo.keys, key) >= 0) { + combos.push(active_combo); } } - } - }; + for (_k = 0, _len1 = combos.length; _k < _len1; _k++) { + combo = combos[_k]; + this._handle_combo_up(combo, e, key); + } + if (active_combos_length > 1) { + _ref3 = this._active_combos; + for (_l = 0, _len2 = _ref3.length; _l < _len2; _l++) { + active_combo = _ref3[_l]; + if (active_combo === void 0 || __indexOf.call(combos, active_combo) >= 0) { + continue; + } + if (!this._keys_remain(active_combo)) { + this._remove_from_active_combos(active_combo); + } + } + } + }; - Listener.prototype._handle_combo_up = function(combo, e, key) { - var keys_down, keys_remaining; - this._prevent_default(e, combo && combo.prevent_default); - keys_remaining = this._keys_remain(combo); - if (!combo.keyup_fired) { - keys_down = this._keys_down.slice(); - keys_down.push(key); - if (!combo.is_solitary || _compare_arrays(keys_down, combo.keys)) { - this._fire("keyup", combo, e); - if (combo.is_counting && typeof combo.on_keyup === "function" && typeof combo.on_keydown !== "function") { - combo.count += 1; + Listener.prototype._handle_combo_up = function(combo, e, key) { + var keys_down, keys_remaining; + this._prevent_default(e, combo && combo.prevent_default); + keys_remaining = this._keys_remain(combo); + if (!combo.keyup_fired) { + keys_down = this._keys_down.slice(); + keys_down.push(key); + if (!combo.is_solitary || _compare_arrays(keys_down, combo.keys)) { + this._fire("keyup", combo, e); + if (combo.is_counting && typeof combo.on_keyup === "function" && typeof combo.on_keydown !== "function") { + combo.count += 1; + } } } - } - if (!keys_remaining) { - this._fire("release", combo, e); - this._remove_from_active_combos(combo); - } - }; + if (!keys_remaining) { + this._fire("release", combo, e); + this._remove_from_active_combos(combo); + } + }; - Listener.prototype.simple_combo = function(keys, callback) { - return this.register_combo({ - keys: keys, - on_keydown: callback - }); - }; + Listener.prototype.simple_combo = function(keys, callback) { + return this.register_combo({ + keys: keys, + on_keydown: callback + }); + }; - Listener.prototype.counting_combo = function(keys, count_callback) { - return this.register_combo({ - keys: keys, - is_counting: true, - is_unordered: false, - on_keydown: count_callback - }); - }; + Listener.prototype.counting_combo = function(keys, count_callback) { + return this.register_combo({ + keys: keys, + is_counting: true, + is_unordered: false, + on_keydown: count_callback + }); + }; - Listener.prototype.sequence_combo = function(keys, callback) { - return this.register_combo({ - keys: keys, - on_keydown: callback, - is_sequence: true, - is_exclusive: true - }); - }; + Listener.prototype.sequence_combo = function(keys, callback) { + return this.register_combo({ + keys: keys, + on_keydown: callback, + is_sequence: true, + is_exclusive: true + }); + }; - Listener.prototype.register_combo = function(combo_dictionary) { - var combo, property, value, _ref; - if (typeof combo_dictionary["keys"] === "string") { - combo_dictionary["keys"] = combo_dictionary["keys"].split(" "); - } - _ref = this._defaults; - for (property in _ref) { - if (!__hasProp.call(_ref, property)) continue; - value = _ref[property]; - if (combo_dictionary[property] === void 0) { - combo_dictionary[property] = value; + Listener.prototype.register_combo = function(combo_dictionary) { + var combo, property, value, _ref; + if (typeof combo_dictionary["keys"] === "string") { + combo_dictionary["keys"] = combo_dictionary["keys"].split(" "); } - } - combo = new Combo(combo_dictionary); - if (_validate_combo(combo)) { - this._registered_combos.push(combo); - return combo; - } - }; + _ref = this._defaults; + for (property in _ref) { + if (!__hasProp.call(_ref, property)) continue; + value = _ref[property]; + if (combo_dictionary[property] === void 0) { + combo_dictionary[property] = value; + } + } + combo = new Combo(combo_dictionary); + if (_validate_combo(combo)) { + this._registered_combos.push(combo); + return combo; + } + }; - Listener.prototype.register_many = function(combo_array) { - var combo, _i, _len, _results; - _results = []; - for (_i = 0, _len = combo_array.length; _i < _len; _i++) { - combo = combo_array[_i]; - _results.push(this.register_combo(combo)); - } - return _results; - }; + Listener.prototype.register_many = function(combo_array) { + var combo, _i, _len, _results; + _results = []; + for (_i = 0, _len = combo_array.length; _i < _len; _i++) { + combo = combo_array[_i]; + _results.push(this.register_combo(combo)); + } + return _results; + }; - Listener.prototype.unregister_combo = function(keys_or_combo) { - var combo, i, unregister_combo, _i, _j, _len, _ref, _ref1, _results; - if (!keys_or_combo) { - return false; - } - unregister_combo = (function(_this) { - return function(combo) { - var i, _i, _ref, _results; + Listener.prototype.unregister_combo = function(keys_or_combo) { + var combo, i, unregister_combo, _i, _j, _len, _ref, _ref1, _results; + if (!keys_or_combo) { + return false; + } + unregister_combo = (function(_this) { + return function(combo) { + var i, _i, _ref, _results; + _results = []; + for (i = _i = 0, _ref = _this._registered_combos.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) { + if (combo === _this._registered_combos[i]) { + _this._registered_combos.splice(i, 1); + break; + } else { + _results.push(void 0); + } + } + return _results; + }; + })(this); + if (keys_or_combo instanceof Combo) { + return unregister_combo(keys_or_combo); + } else { + if (typeof keys_or_combo === "string") { + keys_or_combo = keys_or_combo.split(" "); + for (i = _i = 0, _ref = keys_or_combo.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) { + if (keys_or_combo[i] === "meta") { + keys_or_combo[i] = _metakey; + } + } + } + _ref1 = this._registered_combos; _results = []; - for (i = _i = 0, _ref = _this._registered_combos.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) { - if (combo === _this._registered_combos[i]) { - _this._registered_combos.splice(i, 1); - break; + for (_j = 0, _len = _ref1.length; _j < _len; _j++) { + combo = _ref1[_j]; + if (combo == null) { + continue; + } + if ((combo.is_unordered && _compare_arrays(keys_or_combo, combo.keys)) || (!combo.is_unordered && _compare_arrays_sorted(keys_or_combo, combo.keys))) { + _results.push(unregister_combo(combo)); } else { _results.push(void 0); } } return _results; - }; - })(this); - if (keys_or_combo instanceof Combo) { - return unregister_combo(keys_or_combo); - } else { - if (typeof keys_or_combo === "string") { - keys_or_combo = keys_or_combo.split(" "); - for (i = _i = 0, _ref = keys_or_combo.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) { - if (keys_or_combo[i] === "meta") { - keys_or_combo[i] = _metakey; - } - } } - _ref1 = this._registered_combos; + }; + + Listener.prototype.unregister_many = function(combo_array) { + var combo, _i, _len, _results; _results = []; - for (_j = 0, _len = _ref1.length; _j < _len; _j++) { - combo = _ref1[_j]; - if (combo == null) { - continue; - } - if ((combo.is_unordered && _compare_arrays(keys_or_combo, combo.keys)) || (!combo.is_unordered && _compare_arrays_sorted(keys_or_combo, combo.keys))) { - _results.push(unregister_combo(combo)); - } else { - _results.push(void 0); - } + for (_i = 0, _len = combo_array.length; _i < _len; _i++) { + combo = combo_array[_i]; + _results.push(this.unregister_combo(combo)); } return _results; - } - }; - - Listener.prototype.unregister_many = function(combo_array) { - var combo, _i, _len, _results; - _results = []; - for (_i = 0, _len = combo_array.length; _i < _len; _i++) { - combo = combo_array[_i]; - _results.push(this.unregister_combo(combo)); - } - return _results; - }; + }; - Listener.prototype.get_registered_combos = function() { - return this._registered_combos; - }; + Listener.prototype.get_registered_combos = function() { + return this._registered_combos; + }; - Listener.prototype.reset = function() { - return this._registered_combos = []; - }; + Listener.prototype.reset = function() { + return this._registered_combos = []; + }; - Listener.prototype.listen = function() { - return this._prevent_capture = false; - }; + Listener.prototype.listen = function() { + return this._prevent_capture = false; + }; - Listener.prototype.stop_listening = function() { - return this._prevent_capture = true; - }; + Listener.prototype.stop_listening = function() { + return this._prevent_capture = true; + }; - Listener.prototype.get_meta_key = function() { - return _metakey; - }; + Listener.prototype.get_meta_key = function() { + return _metakey; + }; - return Listener; + return Listener; -})(); + })(); -_decide_meta_key = function() { - if (navigator.userAgent.indexOf("Mac OS X") !== -1) { - _metakey = "cmd"; - } -}; + _decide_meta_key = function() { + if (navigator.userAgent.indexOf("Mac OS X") !== -1) { + _metakey = "cmd"; + } + }; -_change_keycodes_by_browser = function() { - if (navigator.userAgent.indexOf("Opera") !== -1) { - _keycode_dictionary["17"] = "cmd"; - } -}; + _change_keycodes_by_browser = function() { + if (navigator.userAgent.indexOf("Opera") !== -1) { + _keycode_dictionary["17"] = "cmd"; + } + }; -_convert_key_to_readable = function(k) { - return _keycode_dictionary[k]; -}; + _convert_key_to_readable = function(k) { + return _keycode_dictionary[k]; + }; -_filter_array = function(array, callback) { - var element; - if (array.filter) { - return array.filter(callback); - } else { - return (function() { - var _i, _len, _results; - _results = []; - for (_i = 0, _len = array.length; _i < _len; _i++) { - element = array[_i]; - if (callback(element)) { - _results.push(element); + _filter_array = function(array, callback) { + var element; + if (array.filter) { + return array.filter(callback); + } else { + return (function() { + var _i, _len, _results; + _results = []; + for (_i = 0, _len = array.length; _i < _len; _i++) { + element = array[_i]; + if (callback(element)) { + _results.push(element); + } } - } - return _results; - })(); - } -}; - -_compare_arrays = function(a1, a2) { - var item, _i, _len; - if (a1.length !== a2.length) { - return false; - } - for (_i = 0, _len = a1.length; _i < _len; _i++) { - item = a1[_i]; - if (__indexOf.call(a2, item) >= 0) { - continue; + return _results; + })(); } - return false; - } - return true; -}; + }; -_compare_arrays_sorted = function(a1, a2) { - var i, _i, _ref; - if (a1.length !== a2.length) { - return false; - } - for (i = _i = 0, _ref = a1.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) { - if (a1[i] !== a2[i]) { + _compare_arrays = function(a1, a2) { + var item, _i, _len; + if (a1.length !== a2.length) { return false; } - } - return true; -}; - -_is_array_in_array = function(a1, a2) { - var item, _i, _len; - for (_i = 0, _len = a1.length; _i < _len; _i++) { - item = a1[_i]; - if (__indexOf.call(a2, item) < 0) { + for (_i = 0, _len = a1.length; _i < _len; _i++) { + item = a1[_i]; + if (__indexOf.call(a2, item) >= 0) { + continue; + } return false; } - } - return true; -}; - -_index_of_in_array = Array.prototype.indexOf || function(a, item) { - var i, _i, _ref; - for (i = _i = 0, _ref = a.length; 0 <= _ref ? _i <= _ref : _i >= _ref; i = 0 <= _ref ? ++_i : --_i) { - if (a[i] === item) { - return i; - } - } - return -1; -}; - -_is_array_in_array_sorted = function(a1, a2) { - var index, item, prev, _i, _len; - prev = 0; - for (_i = 0, _len = a1.length; _i < _len; _i++) { - item = a1[_i]; - index = _index_of_in_array.call(a2, item); - if (index >= prev) { - prev = index; - } else { + return true; + }; + + _compare_arrays_sorted = function(a1, a2) { + var i, _i, _ref; + if (a1.length !== a2.length) { return false; } - } - return true; -}; + for (i = _i = 0, _ref = a1.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) { + if (a1[i] !== a2[i]) { + return false; + } + } + return true; + }; -_log_error = function() { - if (keypress.debug) { - return console.log.apply(console, arguments); - } -}; - -_key_is_valid = function(key) { - var valid, valid_key, _; - valid = false; - for (_ in _keycode_dictionary) { - valid_key = _keycode_dictionary[_]; - if (key === valid_key) { - valid = true; - break; + _is_array_in_array = function(a1, a2) { + var item, _i, _len; + for (_i = 0, _len = a1.length; _i < _len; _i++) { + item = a1[_i]; + if (__indexOf.call(a2, item) < 0) { + return false; + } } - } - if (!valid) { - for (_ in _keycode_shifted_keys) { - valid_key = _keycode_shifted_keys[_]; + return true; + }; + + _index_of_in_array = Array.prototype.indexOf || function(a, item) { + var i, _i, _ref; + for (i = _i = 0, _ref = a.length; 0 <= _ref ? _i <= _ref : _i >= _ref; i = 0 <= _ref ? ++_i : --_i) { + if (a[i] === item) { + return i; + } + } + return -1; + }; + + _is_array_in_array_sorted = function(a1, a2) { + var index, item, prev, _i, _len; + prev = 0; + for (_i = 0, _len = a1.length; _i < _len; _i++) { + item = a1[_i]; + index = _index_of_in_array.call(a2, item); + if (index >= prev) { + prev = index; + } else { + return false; + } + } + return true; + }; + + _log_error = function() { + if (keypress.debug) { + return console.log.apply(console, arguments); + } + }; + + _key_is_valid = function(key) { + var valid, valid_key, _; + valid = false; + for (_ in _keycode_dictionary) { + valid_key = _keycode_dictionary[_]; if (key === valid_key) { valid = true; break; } } - } - return valid; -}; - -_validate_combo = function(combo) { - var alt_name, i, key, mod_key, non_modifier_keys, property, validated, value, _i, _j, _k, _len, _len1, _ref, _ref1; - validated = true; - if (!combo.keys.length) { - _log_error("You're trying to bind a combo with no keys:", combo); - } - for (i = _i = 0, _ref = combo.keys.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) { - key = combo.keys[i]; - alt_name = _keycode_alternate_names[key]; - if (alt_name) { - key = combo.keys[i] = alt_name; + if (!valid) { + for (_ in _keycode_shifted_keys) { + valid_key = _keycode_shifted_keys[_]; + if (key === valid_key) { + valid = true; + break; + } + } } - if (key === "meta") { - combo.keys.splice(i, 1, _metakey); + return valid; + }; + + _validate_combo = function(combo) { + var alt_name, i, key, mod_key, non_modifier_keys, property, validated, value, _i, _j, _k, _len, _len1, _ref, _ref1; + validated = true; + if (!combo.keys.length) { + _log_error("You're trying to bind a combo with no keys:", combo); + } + for (i = _i = 0, _ref = combo.keys.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) { + key = combo.keys[i]; + alt_name = _keycode_alternate_names[key]; + if (alt_name) { + key = combo.keys[i] = alt_name; + } + if (key === "meta") { + combo.keys.splice(i, 1, _metakey); + } + if (key === "cmd") { + _log_error("Warning: use the \"meta\" key rather than \"cmd\" for Windows compatibility"); + } } - if (key === "cmd") { - _log_error("Warning: use the \"meta\" key rather than \"cmd\" for Windows compatibility"); + _ref1 = combo.keys; + for (_j = 0, _len = _ref1.length; _j < _len; _j++) { + key = _ref1[_j]; + if (!_key_is_valid(key)) { + _log_error("Do not recognize the key \"" + key + "\""); + validated = false; + } } - } - _ref1 = combo.keys; - for (_j = 0, _len = _ref1.length; _j < _len; _j++) { - key = _ref1[_j]; - if (!_key_is_valid(key)) { - _log_error("Do not recognize the key \"" + key + "\""); - validated = false; + if (__indexOf.call(combo.keys, "meta") >= 0 || __indexOf.call(combo.keys, "cmd") >= 0) { + non_modifier_keys = combo.keys.slice(); + for (_k = 0, _len1 = _modifier_keys.length; _k < _len1; _k++) { + mod_key = _modifier_keys[_k]; + if ((i = _index_of_in_array.call(non_modifier_keys, mod_key)) > -1) { + non_modifier_keys.splice(i, 1); + } + } + if (non_modifier_keys.length > 1) { + _log_error("META and CMD key combos cannot have more than 1 non-modifier keys", combo, non_modifier_keys); + validated = false; + } } - } - if (__indexOf.call(combo.keys, "meta") >= 0 || __indexOf.call(combo.keys, "cmd") >= 0) { - non_modifier_keys = combo.keys.slice(); - for (_k = 0, _len1 = _modifier_keys.length; _k < _len1; _k++) { - mod_key = _modifier_keys[_k]; - if ((i = _index_of_in_array.call(non_modifier_keys, mod_key)) > -1) { - non_modifier_keys.splice(i, 1); + for (property in combo) { + value = combo[property]; + if (_factory_defaults[property] === "undefined") { + _log_error("The property " + property + " is not a valid combo property. Your combo has still been registered."); } } - if (non_modifier_keys.length > 1) { - _log_error("META and CMD key combos cannot have more than 1 non-modifier keys", combo, non_modifier_keys); - validated = false; + return validated; + }; + + _convert_to_shifted_key = function(key, e) { + var k; + if (!e.shiftKey) { + return false; } - } - for (property in combo) { - value = combo[property]; - if (_factory_defaults[property] === "undefined") { - _log_error("The property " + property + " is not a valid combo property. Your combo has still been registered."); + k = _keycode_shifted_keys[key]; + if (k != null) { + return k; } - } - return validated; -}; - -_convert_to_shifted_key = function(key, e) { - var k; - if (!e.shiftKey) { return false; + }; + + _modifier_event_mapping = { + "cmd": "metaKey", + "ctrl": "ctrlKey", + "shift": "shiftKey", + "alt": "altKey" + }; + + _keycode_alternate_names = { + "escape": "esc", + "control": "ctrl", + "command": "cmd", + "break": "pause", + "windows": "cmd", + "option": "alt", + "caps_lock": "caps", + "apostrophe": "\'", + "semicolon": ";", + "tilde": "~", + "accent": "`", + "scroll_lock": "scroll", + "num_lock": "num" + }; + + _keycode_shifted_keys = { + "/": "?", + ".": ">", + ",": "<", + "\'": "\"", + ";": ":", + "[": "{", + "]": "}", + "\\": "|", + "`": "~", + "=": "+", + "-": "_", + "1": "!", + "2": "@", + "3": "#", + "4": "$", + "5": "%", + "6": "^", + "7": "&", + "8": "*", + "9": "(", + "0": ")" + }; + + _keycode_dictionary = { + 0: "\\", + 8: "backspace", + 9: "tab", + 12: "num", + 13: "enter", + 16: "shift", + 17: "ctrl", + 18: "alt", + 19: "pause", + 20: "caps", + 27: "esc", + 32: "space", + 33: "pageup", + 34: "pagedown", + 35: "end", + 36: "home", + 37: "left", + 38: "up", + 39: "right", + 40: "down", + 44: "print", + 45: "insert", + 46: "delete", + 48: "0", + 49: "1", + 50: "2", + 51: "3", + 52: "4", + 53: "5", + 54: "6", + 55: "7", + 56: "8", + 57: "9", + 65: "a", + 66: "b", + 67: "c", + 68: "d", + 69: "e", + 70: "f", + 71: "g", + 72: "h", + 73: "i", + 74: "j", + 75: "k", + 76: "l", + 77: "m", + 78: "n", + 79: "o", + 80: "p", + 81: "q", + 82: "r", + 83: "s", + 84: "t", + 85: "u", + 86: "v", + 87: "w", + 88: "x", + 89: "y", + 90: "z", + 91: "cmd", + 92: "cmd", + 93: "cmd", + 96: "num_0", + 97: "num_1", + 98: "num_2", + 99: "num_3", + 100: "num_4", + 101: "num_5", + 102: "num_6", + 103: "num_7", + 104: "num_8", + 105: "num_9", + 106: "num_multiply", + 107: "num_add", + 108: "num_enter", + 109: "num_subtract", + 110: "num_decimal", + 111: "num_divide", + 112: "f1", + 113: "f2", + 114: "f3", + 115: "f4", + 116: "f5", + 117: "f6", + 118: "f7", + 119: "f8", + 120: "f9", + 121: "f10", + 122: "f11", + 123: "f12", + 124: "print", + 144: "num", + 145: "scroll", + 186: ";", + 187: "=", + 188: ",", + 189: "-", + 190: ".", + 191: "/", + 192: "`", + 219: "[", + 220: "\\", + 221: "]", + 222: "\'", + 223: "`", + 224: "cmd", + 225: "alt", + 57392: "ctrl", + 63289: "num", + 59: ";", + 61: "=", + 173: "-" + }; + + keypress._keycode_dictionary = _keycode_dictionary; + + keypress._is_array_in_array_sorted = _is_array_in_array_sorted; + + _decide_meta_key(); + + _change_keycodes_by_browser(); + + if (typeof define === "function" && define.amd) { + define([], function() { + return keypress; + }); + } else if (typeof exports !== "undefined" && exports !== null) { + exports.keypress = keypress; + } else { + window.keypress = keypress; } - k = _keycode_shifted_keys[key]; - if (k != null) { - return k; - } - return false; -}; - -_modifier_event_mapping = { - "cmd": "metaKey", - "ctrl": "ctrlKey", - "shift": "shiftKey", - "alt": "altKey" -}; - -_keycode_alternate_names = { - "escape": "esc", - "control": "ctrl", - "command": "cmd", - "break": "pause", - "windows": "cmd", - "option": "alt", - "caps_lock": "caps", - "apostrophe": "\'", - "semicolon": ";", - "tilde": "~", - "accent": "`", - "scroll_lock": "scroll", - "num_lock": "num" -}; - -_keycode_shifted_keys = { - "/": "?", - ".": ">", - ",": "<", - "\'": "\"", - ";": ":", - "[": "{", - "]": "}", - "\\": "|", - "`": "~", - "=": "+", - "-": "_", - "1": "!", - "2": "@", - "3": "#", - "4": "$", - "5": "%", - "6": "^", - "7": "&", - "8": "*", - "9": "(", - "0": ")" -}; - -_keycode_dictionary = { - 0: "\\", - 8: "backspace", - 9: "tab", - 12: "num", - 13: "enter", - 16: "shift", - 17: "ctrl", - 18: "alt", - 19: "pause", - 20: "caps", - 27: "esc", - 32: "space", - 33: "pageup", - 34: "pagedown", - 35: "end", - 36: "home", - 37: "left", - 38: "up", - 39: "right", - 40: "down", - 44: "print", - 45: "insert", - 46: "delete", - 48: "0", - 49: "1", - 50: "2", - 51: "3", - 52: "4", - 53: "5", - 54: "6", - 55: "7", - 56: "8", - 57: "9", - 65: "a", - 66: "b", - 67: "c", - 68: "d", - 69: "e", - 70: "f", - 71: "g", - 72: "h", - 73: "i", - 74: "j", - 75: "k", - 76: "l", - 77: "m", - 78: "n", - 79: "o", - 80: "p", - 81: "q", - 82: "r", - 83: "s", - 84: "t", - 85: "u", - 86: "v", - 87: "w", - 88: "x", - 89: "y", - 90: "z", - 91: "cmd", - 92: "cmd", - 93: "cmd", - 96: "num_0", - 97: "num_1", - 98: "num_2", - 99: "num_3", - 100: "num_4", - 101: "num_5", - 102: "num_6", - 103: "num_7", - 104: "num_8", - 105: "num_9", - 106: "num_multiply", - 107: "num_add", - 108: "num_enter", - 109: "num_subtract", - 110: "num_decimal", - 111: "num_divide", - 112: "f1", - 113: "f2", - 114: "f3", - 115: "f4", - 116: "f5", - 117: "f6", - 118: "f7", - 119: "f8", - 120: "f9", - 121: "f10", - 122: "f11", - 123: "f12", - 124: "print", - 144: "num", - 145: "scroll", - 186: ";", - 187: "=", - 188: ",", - 189: "-", - 190: ".", - 191: "/", - 192: "`", - 219: "[", - 220: "\\", - 221: "]", - 222: "\'", - 223: "`", - 224: "cmd", - 225: "alt", - 57392: "ctrl", - 63289: "num", - 59: ";", - 61: "=", - 173: "-" -}; - -keypress._keycode_dictionary = _keycode_dictionary; - -keypress._is_array_in_array_sorted = _is_array_in_array_sorted; - -_decide_meta_key(); - -_change_keycodes_by_browser(); - -if (typeof define === "function" && define.amd) { - define([], function() { - return keypress; - }); -} else if (typeof exports !== "undefined" && exports !== null) { - exports.keypress = keypress; -} else { - window.keypress = keypress; -} + +}).call(this); diff --git a/package.json b/package.json index e167d71..ba4c972 100644 --- a/package.json +++ b/package.json @@ -1,13 +1,13 @@ { "name" : "keypress.js", - "version" : "2.1.4", - "main" : "keypress-2.1.4.min.js", + "version" : "2.1.5", + "main" : "keypress-2.1.5.min.js", "description" : "a robust keyboard input capturing Javascript utility focused on input for games. For details and documentation, please visit http://dmauro.github.io/Keypress/", "devDependencies" : { "jasmine" : "1.3.0" }, "spm": { - "main": "keypress-2.1.4.min.js", + "main": "keypress-2.1.5.min.js", "ignore": [ "compiler.jar" ]