From d04aec5109a29754e4d7808bf60a30e28b46a0f9 Mon Sep 17 00:00:00 2001 From: David Mauro Date: Wed, 5 Feb 2014 12:23:03 -0500 Subject: [PATCH] More issue #27 fixes. --- keypress-2.0.1.min.js | 52 +++++++++++++++++++++---------------------- keypress.coffee | 7 +++--- keypress.js | 5 ++++- 3 files changed, 33 insertions(+), 31 deletions(-) diff --git a/keypress-2.0.1.min.js b/keypress-2.0.1.min.js index be5c470..8dbe905 100644 --- a/keypress-2.0.1.min.js +++ b/keypress-2.0.1.min.js @@ -3,29 +3,29 @@ Licensed under the Apache License, Version 2.0 http://www.apache.org/licenses/LICENSE-2.0 */ -(function(){var u,w,x,y,q,v,C,D,E,F,r,s,m,p,t,z,G,A={}.hasOwnProperty,i=[].indexOf||function(a){for(var c=0,b=this.length;ci.call(a,"cmd")?!1:!0};f.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()};f.prototype._get_active_combos=function(a){var c,b,d=this; -c=[];b=v(this._keys_down,function(b){return b!==a});b.push(a);this._match_combo_arrays(b,function(a){if(d._cmd_bug_check(a.keys))return c.push(a)});this._fuzzy_match_combo_arrays(b,function(a){if(!(0<=i.call(c,a))&&!a.is_solitary&&d._cmd_bug_check(a.keys))return c.push(a)});return c};f.prototype._get_potential_combos=function(a){var c,b,d,e,g;b=[];g=this._registered_combos;d=0;for(e=g.length;dk;e=0<=k?++h:--h)if((c=this._active_combos[e])&&c.is_exclusive&&a.is_exclusive){c=c.keys;if(!j){f=0;for(n=c.length;fi.call(a.keys,b)){j=!1;break}}if(g&&!j){l=a.keys;f=0;for(n=l.length;fi.call(c,b)){g=!0;break}}j&&(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),g=!1)}}g&&this._active_combos.unshift(a);return j||g};f.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}};f.prototype._get_possible_sequences=function(){var a,c,b,d,e,g,j,h,f,n,k,l;d=[];n=this._registered_combos;g=0;for(f=n.length;g=k;c=1<=k?++j:--j)if(e=this._sequence.slice(-c),a.is_sequence){if(0>i.call(a.keys,"shift")&&(e=v(e,function(a){return"shift"!==a}),!e.length))continue;c=h=0;for(l=e.length;0<=l?hl;c=0<=l?++h:--h)if(a.keys[c]===e[c])b=!0;else{b=!1;break}b&&d.push(a)}}return d};f.prototype._add_key_to_sequence=function(a,c){var b,d,e,g;this._sequence.push(a);d=this._get_possible_sequences();if(d.length){e=0;for(g=d.length;e=l;b=1<=l?++f:--f)if(g=v(this._sequence,function(a){return 0<=i.call(c.keys,"shift")?!0:"shift"!==a}).slice(-b),c.keys.length===g.length){b= -o=0;for(m=g.length;0<=m?om;b=0<=m?++o:--o)if(e=g[b],!(0>i.call(c.keys,"shift")&&"shift"===e)&&!("shift"===a&&0>i.call(c.keys,"shift")))if(c.keys[b]===e)d=!0;else{d=!1;break}}if(d)return c}return!1};f.prototype._receive_input=function(a,c){var b;if(this._prevent_capture)this._keys_down.length&&(this._keys_down=[]);else if(b=x(a.keyCode),(c||this._keys_down.length||!("alt"===b||b===p))&&b)return c?this._key_down(b,a):this._key_up(b,a)};f.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};f.prototype._match_combo_arrays=function(a,c){var b,d,e,g;g=this._registered_combos;d=0;for(e=g.length;dg;b=0<=g?++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);g=0;for(j=d.length;gi.call(this._keys_down,a)&&this._keys_down.push(a)};f.prototype._handle_combo_down=function(a,c,b,d){var e,g,j,f,o;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;j=this._add_to_active_combos(a,b);b=a.keyup_fired=!1;if(a.is_exclusive){f=0;for(o=c.length;fa.keys.length){b=!0;break}}if(!b&&(a.is_counting&&"function"===typeof a.on_keydown&&(a.count+=1),j))return this._fire("keydown",a,d,e)};f.prototype._key_up=function(a,c){var b,d,e,g,f,h;b=a;(e=y(a,c))&&(a=e);e=s[b];c.shiftKey?e&&0<=i.call(this._keys_down,e)||(a=b):b&&0<=i.call(this._keys_down,b)||(a=e);(g=this._get_sequence(a))&& -this._fire("keyup",g,c);if(0>i.call(this._keys_down,a))return!1;g=f=0;for(h=this._keys_down.length;0<=h?fh;g=0<=h?++f:--f)if((d=this._keys_down[g])===a||d===e||d===b){this._keys_down.splice(g,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===f._registered_combos[b]){f._registered_combos.splice(b,1);break}else e.push(void 0);return e};if(a.keys)return b(a);g=this._registered_combos;d=0;for(e=g.length;de;b=0<=e?++d:--d)if(a[b]!==c[b])return!1;return!0};C=function(a,c){var b,d,e;d=0;for(e=a.length;d< -e;d++)if(b=a[d],0>i.call(c,b))return!1;return!0};D=function(a,c){var b,d,e,g;e=d=0;for(g=a.length;e=d)d=b;else return!1;return!0};m=function(){if(keypress.debug)return console.log.apply(console,arguments)};E=function(a){var c,b,d;c=!1;for(d in r)if(b=r[d],a===b){c=!0;break}if(!c)for(d in s)if(b=s[d],a===b){c=!0;break}return c};G=function(a){var c,b,d,e,g,f,h;g=!0;a.keys.length||m("You're trying to bind a combo with no keys:",a);b=f=0;for(h=a.keys.length;0<=h?fh;b=0<=h?++f:--f)d=a.keys[b],(c=F[d])&&(d=a.keys[b]=c),"meta"===d&&a.keys.splice(b,1,p),"cmd"===d&&m('Warning: use the "meta" key rather than "cmd" for Windows compatibility');h=a.keys;c=0;for(f=h.length;c",",":"<","'":'"', -";":":","[":"{","]":"}","\\":"|","`":"~","=":"+","-":"_",1:"!",2:"@",3:"#",4:"$",5:"%",6:"^",7:"&",8:"*",9:"(","0":")"};r={"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",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"};-1!==navigator.userAgent.indexOf("Mac OS X")&&(p="cmd");-1!==navigator.userAgent.indexOf("Opera")&&(r["17"]="cmd")}).call(this); +(function(){var u,w,x,y,p,v,C,D,E,F,q,r,o,k,s,z,G,A={}.hasOwnProperty,i=[].indexOf||function(a){for(var c=0,b=this.length;ci.call(a,"cmd")?!1:!0};f.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()};f.prototype._get_active_combos=function(a){var c, +b,d=this;c=[];b=v(this._keys_down,function(b){return b!==a});b.push(a);this._match_combo_arrays(b,function(a){if(d._cmd_bug_check(a.keys))return c.push(a)});this._fuzzy_match_combo_arrays(b,function(a){if(!(0<=i.call(c,a))&&!a.is_solitary&&d._cmd_bug_check(a.keys))return c.push(a)});return c};f.prototype._get_potential_combos=function(a){var c,b,d,e,g;b=[];g=this._registered_combos;d=0;for(e=g.length;dl;e=0<=l?++h:--h)if((c=this._active_combos[e])&&c.is_exclusive&&a.is_exclusive){c=c.keys;if(!j){f=0;for(n=c.length;fi.call(a.keys,b)){j=!1;break}}if(g&&!j){m=a.keys;f=0;for(n=m.length;fi.call(c,b)){g=!0;break}}j&&(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),g=!1)}}g&&this._active_combos.unshift(a);return j||g};f.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}};f.prototype._get_possible_sequences=function(){var a,c,b,d,e,g,j,h,f,n,l,m;d=[];n=this._registered_combos;g=0;for(f=n.length;g=l;c=1<=l?++j:--j)if(e=this._sequence.slice(-c),a.is_sequence){if(0>i.call(a.keys,"shift")&&(e=v(e,function(a){return"shift"!==a}),!e.length))continue;c=h=0;for(m=e.length;0<=m?hm;c=0<=m?++h:--h)if(a.keys[c]===e[c])b=!0;else{b=!1;break}b&&d.push(a)}}return d};f.prototype._add_key_to_sequence=function(a,c){var b,d,e,g;this._sequence.push(a);d=this._get_possible_sequences();if(d.length){e=0;for(g=d.length;e=m;b=1<=m?++f:--f)if(g=v(this._sequence,function(a){return 0<=i.call(c.keys,"shift")?!0:"shift"!== +a}).slice(-b),c.keys.length===g.length){b=t=0;for(k=g.length;0<=k?tk;b=0<=k?++t:--t)if(e=g[b],!(0>i.call(c.keys,"shift")&&"shift"===e)&&!("shift"===a&&0>i.call(c.keys,"shift")))if(c.keys[b]===e)d=!0;else{d=!1;break}}if(d)return c}return!1};f.prototype._receive_input=function(a,c){var b;if(this._prevent_capture)this._keys_down.length&&(this._keys_down=[]);else if(b=x(a.keyCode),(c||this._keys_down.length||!("alt"===b||b===k))&&b)return c?this._key_down(b,a):this._key_up(b,a)};f.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};f.prototype._match_combo_arrays=function(a,c){var b,d,e,g;g=this._registered_combos;d=0;for(e=g.length;dg;b=0<=g?++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);g=0;for(j=d.length;gi.call(this._keys_down,a)&&this._keys_down.push(a)};f.prototype._handle_combo_down=function(a,c,b,d){var e,g,j,f,k;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;j=this._add_to_active_combos(a,b);b=a.keyup_fired=!1;if(a.is_exclusive){f=0;for(k=c.length;fa.keys.length){b=!0;break}}if(!b&&(a.is_counting&&"function"===typeof a.on_keydown&&(a.count+=1),j))return this._fire("keydown",a,d,e)};f.prototype._key_up=function(a,c){var b,d,e,g,f,h;b=a;(e=y(a,c))&&(a=e);e=r[b];c.shiftKey?e&&0<=i.call(this._keys_down,e)||(a=b): +b&&0<=i.call(this._keys_down,b)||(a=e);(g=this._get_sequence(a))&&this._fire("keyup",g,c);if(0>i.call(this._keys_down,a))return!1;g=f=0;for(h=this._keys_down.length;0<=h?fh;g=0<=h?++f:--f)if((d=this._keys_down[g])===a||d===e||d===b){this._keys_down.splice(g,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===f._registered_combos[b]){f._registered_combos.splice(b,1);break}else e.push(void 0);return e};if(a.keys)return b(a);g=this._registered_combos;d=0;for(e=g.length;de;b=0<=e?++d:--d)if(a[b]!==c[b])return!1;return!0};C=function(a, +c){var b,d,e;d=0;for(e=a.length;di.call(c,b))return!1;return!0};D=function(a,c){var b,d,e,g;e=d=0;for(g=a.length;e=d)d=b;else return!1;return!0};o=function(){if(keypress.debug)return console.log.apply(console,arguments)};E=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 r)if(b=r[d],a===b){c=!0;break}return c};G=function(a){var c,b,d,e,g,f,h;g=!0;a.keys.length||o("You're trying to bind a combo with no keys:",a);b= +f=0;for(h=a.keys.length;0<=h?fh;b=0<=h?++f:--f)d=a.keys[b],(c=F[d])&&(d=a.keys[b]=c),"meta"===d&&a.keys.splice(b,1,k),"cmd"===d&&o('Warning: use the "meta" key rather than "cmd" for Windows compatibility');h=a.keys;c=0;for(f=h.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",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"};-1!==navigator.userAgent.indexOf("Mac OS X")&&(k="cmd");-1!==navigator.userAgent.indexOf("Opera")&&(q["17"]="cmd")}).call(this); diff --git a/keypress.coffee b/keypress.coffee index 5079d68..15ece5f 100644 --- a/keypress.coffee +++ b/keypress.coffee @@ -132,14 +132,11 @@ class keypress.Listener # Perhaps we should fire keyup on all active combos when we press cmd? _cmd_bug_check: (combo_keys) -> - - # TODO: Document this properly - # We don't want to allow combos to activate if the cmd key # is pressed, but cmd isn't in them. This is so they don't # accidentally rapid fire due to our hack-around for the cmd # key bug and having to fake keyups. - if "cmd" in @_keys_down and "cmd" not in combo_keys + if _metakey is "cmd" and "cmd" in @_keys_down and "cmd" not in combo_keys return false return true @@ -379,6 +376,8 @@ class keypress.Listener for mod, event_mod of _modifier_event_mapping continue if mod is key if mod in @_keys_down and not e[event_mod] + # The Windows key will think it is the cmd key, but won't trigger the event mod + continue if mod is "cmd" and _metakey isnt "cmd" for i in [0...@_keys_down.length] @_keys_down.splice(i, 1) if @_keys_down[i] is mod diff --git a/keypress.js b/keypress.js index e9b3a12..8d2c859 100644 --- a/keypress.js +++ b/keypress.js @@ -143,7 +143,7 @@ Combo options available and their defaults: }; Listener.prototype._cmd_bug_check = function(combo_keys) { - if (__indexOf.call(this._keys_down, "cmd") >= 0 && __indexOf.call(combo_keys, "cmd") < 0) { + if (_metakey === "cmd" && __indexOf.call(this._keys_down, "cmd") >= 0 && __indexOf.call(combo_keys, "cmd") < 0) { return false; } return true; @@ -466,6 +466,9 @@ Combo options available and their defaults: 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);