From 4888184b36e0eaad7de07372d8d3868f1f04a11e Mon Sep 17 00:00:00 2001 From: David Mauro Date: Tue, 29 Jul 2014 22:55:54 -0400 Subject: [PATCH 01/12] Fix some keyCodes in Firefox --- README.md | 4 ++++ keypress.coffee | 2 ++ 2 files changed, 6 insertions(+) diff --git a/README.md b/README.md index bbd0c3d..70fd5bd 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,10 @@ released under the Apache License, version 2.0 **What's new** --------------- +2.0.4 + +* Fixed compatibility for '-'/'_' and '='/'+' keys in FireFox. Thanks to [simonsarris](https://github.com/simonsarris) for spotting the bug. [Issue #50](https://github.com/dmauro/Keypress/issues/50) + 2.03 * Fixed a bug unregistering combos using arrays of keys diff --git a/keypress.coffee b/keypress.coffee index 99f3414..2a9b73c 100644 --- a/keypress.coffee +++ b/keypress.coffee @@ -870,6 +870,8 @@ _keycode_dictionary = 63289 : "num" # Firefox weirdness 59 : ";" + 61 : "-" + 173 : "=" ############ # Initialize From 6bd3175249070d2218fc2be30f8719ee8cc1264b Mon Sep 17 00:00:00 2001 From: David Mauro Date: Tue, 29 Jul 2014 22:56:59 -0400 Subject: [PATCH 02/12] Typo fix in README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 70fd5bd..a102657 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ released under the Apache License, version 2.0 * Fixed compatibility for '-'/'_' and '='/'+' keys in FireFox. Thanks to [simonsarris](https://github.com/simonsarris) for spotting the bug. [Issue #50](https://github.com/dmauro/Keypress/issues/50) -2.03 +2.0.3 * Fixed a bug unregistering combos using arrays of keys * Added ie8 compatibility shim. Thanks to [barrkel](https://github.com/barrkel). [Issue #41](https://github.com/dmauro/Keypress/issues/41) From e9a281226907aab5033d6914e0cad25a76caef6d Mon Sep 17 00:00:00 2001 From: Steve Erickson Date: Sat, 11 Oct 2014 16:40:09 -0400 Subject: [PATCH 03/12] add "destroy" method Add a public destroy method which will unbind event listeners --- keypress.coffee | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/keypress.coffee b/keypress.coffee index 2a9b73c..f06b0e7 100644 --- a/keypress.coffee +++ b/keypress.coffee @@ -98,7 +98,7 @@ class keypress.Listener @_defaults[property] = @_defaults[property] or value # Attach handlers to element - element = element or document.body + @element = element or document.body attach_handler = (target, event, handler) -> if target.addEventListener @@ -106,20 +106,35 @@ class keypress.Listener else if target.attachEvent target.attachEvent "on#{event}", handler - attach_handler element, "keydown", (e) => + handler + + @keydown_event = attach_handler @element, "keydown", (e) => e = e or window.event @_receive_input e, true @_bug_catcher e - attach_handler element, "keyup", (e) => + + @keyup_event = attach_handler @element, "keyup", (e) => e = e or window.event @_receive_input e, false - attach_handler window, "blur", => + + @blur_event = attach_handler window, "blur", => # Assume all keys are released when we can't catch key events # This prevents alt+tab conflicts for key in @_keys_down @_key_up key, {} @_keys_down = [] + destroy: () -> + remove_handler = (target, event, handler) -> + if target.removeEventListener + target.removeEventListener event, handler + else if target.removeEvent + target.removeEvent "on#{event}", handler + + remove_handler @element, "keydown", @keydown_event + remove_handler @element, "keyup", @keyup_event + remove_handler window, "blur", @blur_event + # Helper Methods _bug_catcher: (e) -> From c155c8e7d922ea773a101a47e14d3d3290936efd Mon Sep 17 00:00:00 2001 From: chencheng Date: Tue, 14 Oct 2014 17:50:27 +0800 Subject: [PATCH 04/12] Add spm support --- package.json | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 package.json diff --git a/package.json b/package.json new file mode 100644 index 0000000..02baacf --- /dev/null +++ b/package.json @@ -0,0 +1,15 @@ +{ + "name" : "keypress", + "version" : "2.0.1", + "main" : "keypress.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.js", + "ignore": [ + "compiler.jar" + ] + } +} From 3399aef1e99b522d4eba6ffba04c215183629da9 Mon Sep 17 00:00:00 2001 From: David Mauro Date: Sat, 1 Nov 2014 17:33:51 -0400 Subject: [PATCH 05/12] Update bower json --- bower.json | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/bower.json b/bower.json index 0dac925..1de1df9 100644 --- a/bower.json +++ b/bower.json @@ -1,9 +1,28 @@ { - "name" : "Keypress", - "version" : "2.0.3", - "main" : "keypress.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" + "name": "Keypress", + "main": "keypress-2.0.3.min.js", + "version": "2.0.3", + "homepage": "https://github.com/dmauro/Keypress", + "authors": [ + "David Mauro " + ], + "description": "A robust keyboard input capturing Javascript utility focused on input for games. For details and documentation, please visit http://dmauro.github.io/Keypress", + "moduleType": [ + "amd", + "globals" + ], + "license": "Apache 2.0", + "ignore": [ + "**/.*", + "node_modules", + "bower_components", + "test", + "coffee", + "js", + "*.sh", + "*.jar" + ], + "devDependencies": { + "jasmine": "git://github.com/pivotal/jasmine.git#1.3.0" } } From e1b7a8f626126dd56f4a84183d1a9e085693dc50 Mon Sep 17 00:00:00 2001 From: chencheng Date: Sun, 2 Nov 2014 11:26:22 +0800 Subject: [PATCH 06/12] Update package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 02baacf..674438c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name" : "keypress", - "version" : "2.0.1", + "version" : "2.0.3", "main" : "keypress.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" : { From be89e4b308ebb61e74834d6de670e8bb4176c025 Mon Sep 17 00:00:00 2001 From: David Mauro Date: Sun, 2 Nov 2014 01:30:56 -0500 Subject: [PATCH 07/12] Update changelog --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index a102657..bb09bef 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,7 @@ released under the Apache License, version 2.0 2.0.4 * Fixed compatibility for '-'/'_' and '='/'+' keys in FireFox. Thanks to [simonsarris](https://github.com/simonsarris) for spotting the bug. [Issue #50](https://github.com/dmauro/Keypress/issues/50) +* Added []spm support](http://spmjs.io/package/keypress). Thanks to [sorrycc](https://github.com/sorrycc). [Pull request #52](https://github.com/dmauro/Keypress/pull/52) 2.0.3 From f1b0a4181c5fbfc3924b598aa71b5538372e4d4e Mon Sep 17 00:00:00 2001 From: Dan Rocha Date: Fri, 7 Nov 2014 14:53:20 -0600 Subject: [PATCH 08/12] Support for F# buttons --- keypress-2.0.3.min.js | 4 ++-- keypress.coffee | 14 +++++++++++++- keypress.js | 18 ++++++++++++++++-- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/keypress-2.0.3.min.js b/keypress-2.0.3.min.js index bf3b1d3..2c0ec13 100644 --- a/keypress-2.0.3.min.js +++ b/keypress-2.0.3.min.js @@ -28,5 +28,5 @@ arguments)};G=function(a){var c,b,d;c=!1;for(d in r)if(b=r[d],a===b){c=!0;break} d+'"'),f=!1);if(0<=j.call(a.keys,"meta")||0<=j.call(a.keys,"cmd")){c=a.keys.slice();g=0;for(i=B.length;g",",":"<","'":'"',";":":","[":"{","]":"}","\\":"|","`":"~","=":"+","-":"_",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",59:";"};-1!==navigator.userAgent.indexOf("Mac OS X")&&(m="cmd");-1!==navigator.userAgent.indexOf("Opera")&&(r["17"]="cmd");"function"===typeof define&&define.amd?define([],function(){return o}):"undefined"!==typeof exports&&null!==exports?exports.keypress= -o:window.keypress=o}).call(this); +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:"="};-1!==navigator.userAgent.indexOf("Mac OS X")&&(m="cmd");-1!==navigator.userAgent.indexOf("Opera")&&(r["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 f06b0e7..3060c86 100644 --- a/keypress.coffee +++ b/keypress.coffee @@ -862,7 +862,19 @@ _keycode_dictionary = 108 : "num_enter" 109 : "num_subtract" 110 : "num_decimal" - 111 : "num_divide" + 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" diff --git a/keypress.js b/keypress.js index 464b93a..de81830 100644 --- a/keypress.js +++ b/keypress.js @@ -1,4 +1,4 @@ -// Generated by CoffeeScript 1.7.1 +// Generated by CoffeeScript 1.8.0 /* Copyright 2014 David Mauro @@ -1060,6 +1060,18 @@ Combo options available and their defaults: 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", @@ -1079,7 +1091,9 @@ Combo options available and their defaults: 225: "alt", 57392: "ctrl", 63289: "num", - 59: ";" + 59: ";", + 61: "-", + 173: "=" }; _decide_meta_key(); From cf5dba4dbd810849886c137ab5e15b90fbfe9a54 Mon Sep 17 00:00:00 2001 From: David Mauro Date: Sun, 9 Nov 2014 13:54:54 -0500 Subject: [PATCH 09/12] PR cleanup --- keypress.coffee | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/keypress.coffee b/keypress.coffee index 3060c86..f15fed9 100644 --- a/keypress.coffee +++ b/keypress.coffee @@ -106,7 +106,7 @@ class keypress.Listener else if target.attachEvent target.attachEvent "on#{event}", handler - handler + return handler @keydown_event = attach_handler @element, "keydown", (e) => e = e or window.event @@ -126,9 +126,9 @@ class keypress.Listener destroy: () -> remove_handler = (target, event, handler) -> - if target.removeEventListener + if target.removeEventListener? target.removeEventListener event, handler - else if target.removeEvent + else if target.removeEvent? target.removeEvent "on#{event}", handler remove_handler @element, "keydown", @keydown_event From 840fba8832b380b82d715de34d70a5efcf6166f8 Mon Sep 17 00:00:00 2001 From: David Mauro Date: Sun, 9 Nov 2014 13:59:49 -0500 Subject: [PATCH 10/12] Update to 2.1.0 --- README.md | 5 +++-- bower.json | 4 ++-- coffee-compiler.sh | 2 +- keypress-2.0.3.min.js | 32 -------------------------------- keypress-2.1.0.min.js | 33 +++++++++++++++++++++++++++++++++ keypress.coffee | 2 +- keypress.js | 29 ++++++++++++++++++++++------- package.json | 2 +- 8 files changed, 63 insertions(+), 46 deletions(-) delete mode 100644 keypress-2.0.3.min.js create mode 100644 keypress-2.1.0.min.js diff --git a/README.md b/README.md index bb09bef..779247a 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ Keypress ======== -Version 2.0.3 +Version 2.1.0 Keypress is a robust keyboard input capturing Javascript utility focused on input for games. For details and documentation, please @@ -13,8 +13,9 @@ released under the Apache License, version 2.0 **What's new** --------------- -2.0.4 +2.1.0 +* Added a destroy method to cleanup a listener. Thanks to [smerickson](https://github.com/smerickson) for submitting. [Pull request #51](https://github.com/dmauro/Keypress/pull/51) * Fixed compatibility for '-'/'_' and '='/'+' keys in FireFox. Thanks to [simonsarris](https://github.com/simonsarris) for spotting the bug. [Issue #50](https://github.com/dmauro/Keypress/issues/50) * Added []spm support](http://spmjs.io/package/keypress). Thanks to [sorrycc](https://github.com/sorrycc). [Pull request #52](https://github.com/dmauro/Keypress/pull/52) diff --git a/bower.json b/bower.json index 1de1df9..305574b 100644 --- a/bower.json +++ b/bower.json @@ -1,7 +1,7 @@ { "name": "Keypress", - "main": "keypress-2.0.3.min.js", - "version": "2.0.3", + "main": "keypress-2.1.0.min.js", + "version": "2.1.0", "homepage": "https://github.com/dmauro/Keypress", "authors": [ "David Mauro " diff --git a/coffee-compiler.sh b/coffee-compiler.sh index fa7e340..5a988c0 100755 --- a/coffee-compiler.sh +++ b/coffee-compiler.sh @@ -1,5 +1,5 @@ #!/bin/bash -VERSION="2.0.3" +VERSION="2.1.0" YEAR="2014" cd "$( cd "$( dirname "$0" )" && pwd )" coffee -c keypress.coffee diff --git a/keypress-2.0.3.min.js b/keypress-2.0.3.min.js deleted file mode 100644 index 2c0ec13..0000000 --- a/keypress-2.0.3.min.js +++ /dev/null @@ -1,32 +0,0 @@ -/* - Keypress version 2.0.3 (c) 2014 David Mauro. - Licensed under the Apache License, Version 2.0 - http://www.apache.org/licenses/LICENSE-2.0 -*/ -(function(){var o,v,x,y,z,q,w,A,E,F,G,H,r,s,p,m,t,B,I,C={}.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}return!1};g.prototype._receive_input=function(a,c){var b;if(this._prevent_capture)this._keys_down.length&&(this._keys_down=[]);else if(b=y(a.keyCode),(c||this._keys_down.length||!("alt"===b||b===m))&&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= -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(null!=a.keys)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};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};F=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)};G=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};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,m),"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":")"};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",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:"="};-1!==navigator.userAgent.indexOf("Mac OS X")&&(m="cmd");-1!==navigator.userAgent.indexOf("Opera")&&(r["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.0.min.js b/keypress-2.1.0.min.js new file mode 100644 index 0000000..6d5b0fd --- /dev/null +++ b/keypress-2.1.0.min.js @@ -0,0 +1,33 @@ +/* + Keypress version 2.1.0 (c) 2014 David Mauro. + Licensed under the Apache License, Version 2.0 + http://www.apache.org/licenses/LICENSE-2.0 +*/ +(function(){var o,v,x,y,z,q,w,A,E,F,G,H,r,s,p,m,t,B,I,C={}.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}return!1};g.prototype._receive_input=function(a,c){var b;if(this._prevent_capture)this._keys_down.length&&(this._keys_down=[]);else if(b=y(a.keyCode),(c||this._keys_down.length||!("alt"===b||b===m))&&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=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(null!=a.keys)return b(a);"string"===typeof a&&(a=a.split(" "));f=this._registered_combos;g=[];d=0;for(e=f.length;d< +e;d++)c=f[d],null!=c&&(c.is_unordered&&v(a,c.keys)||!c.is_unordered&&x(a,c.keys)?g.push(b(c)):g.push(void 0));return g};g.prototype.unregister_many=function(a){var c,b,d,e;e=[];b=0;for(d=a.length;be;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};F=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)};G=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};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,m),"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":")"};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", +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:"="};-1!==navigator.userAgent.indexOf("Mac OS X")&&(m="cmd");-1!==navigator.userAgent.indexOf("Opera")&&(r["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 f15fed9..3c4579e 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.0.3 +version 2.1.0 ### ### diff --git a/keypress.js b/keypress.js index de81830..dbb5957 100644 --- a/keypress.js +++ b/keypress.js @@ -18,7 +18,7 @@ limitations under the License. Keypress is a robust keyboard input capturing Javascript utility focused on input for games. -version 2.0.3 +version 2.1.0 */ @@ -106,28 +106,29 @@ Combo options available and their defaults: value = _factory_defaults[property]; this._defaults[property] = this._defaults[property] || value; } - element = element || document.body; + this.element = element || document.body; attach_handler = function(target, event, handler) { if (target.addEventListener) { - return target.addEventListener(event, handler); + target.addEventListener(event, handler); } else if (target.attachEvent) { - return target.attachEvent("on" + event, handler); + target.attachEvent("on" + event, handler); } + return handler; }; - attach_handler(element, "keydown", (function(_this) { + 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)); - attach_handler(element, "keyup", (function(_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)); - attach_handler(window, "blur", (function(_this) { + this.blur_event = attach_handler(window, "blur", (function(_this) { return function() { var key, _i, _len, _ref; _ref = _this._keys_down; @@ -140,6 +141,20 @@ Combo options available and their defaults: })(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; if (_metakey === "cmd" && __indexOf.call(this._keys_down, "cmd") >= 0 && ((_ref = _convert_key_to_readable(e.keyCode)) !== "cmd" && _ref !== "shift" && _ref !== "alt" && _ref !== "caps" && _ref !== "tab")) { diff --git a/package.json b/package.json index 674438c..e9e6162 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name" : "keypress", - "version" : "2.0.3", + "version" : "2.1.0", "main" : "keypress.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" : { From 3243844b50379b3e734981c61e4724760355e9ca Mon Sep 17 00:00:00 2001 From: David Mauro Date: Sun, 9 Nov 2014 14:29:53 -0500 Subject: [PATCH 11/12] Another fix for unregistering array of keys --- README.md | 1 + keypress-2.1.0.min.js | 36 ++++++++++++++++++------------------ keypress.coffee | 6 +++++- keypress.js | 6 +++++- 4 files changed, 29 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 779247a..7299b42 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ released under the Apache License, version 2.0 2.1.0 +* Another fix for unregistering combos using an array of keys * Added a destroy method to cleanup a listener. Thanks to [smerickson](https://github.com/smerickson) for submitting. [Pull request #51](https://github.com/dmauro/Keypress/pull/51) * Fixed compatibility for '-'/'_' and '='/'+' keys in FireFox. Thanks to [simonsarris](https://github.com/simonsarris) for spotting the bug. [Issue #50](https://github.com/dmauro/Keypress/issues/50) * Added []spm support](http://spmjs.io/package/keypress). Thanks to [sorrycc](https://github.com/sorrycc). [Pull request #52](https://github.com/dmauro/Keypress/pull/52) diff --git a/keypress-2.1.0.min.js b/keypress-2.1.0.min.js index 6d5b0fd..8cc9e4e 100644 --- a/keypress-2.1.0.min.js +++ b/keypress-2.1.0.min.js @@ -3,31 +3,31 @@ Licensed under the Apache License, Version 2.0 http://www.apache.org/licenses/LICENSE-2.0 */ -(function(){var o,v,x,y,z,q,w,A,E,F,G,H,r,s,p,m,t,B,I,C={}.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, +h._key_up(a,{});return h._keys_down=[]})};g.prototype.destroy=function(){var a;a=function(a,b,d){if(null!=a.removeEventListener)return a.removeEventListener(b,d);if(null!=a.removeEvent)return a.removeEvent("on"+b,d)};a(this.element,"keydown",this.keydown_event);a(this.element,"keyup",this.keyup_event);return a(window,"blur",this.blur_event)};g.prototype._bug_catcher=function(a){var c;if("cmd"===o&&0<=j.call(this._keys_down,"cmd")&&"cmd"!==(c=z(a.keyCode))&&"shift"!==c&&"alt"!==c&&"caps"!==c&&"tab"!== +c)return this._receive_input(a,!1)};g.prototype._cmd_bug_check=function(a){return"cmd"===o&&0<=j.call(this._keys_down,"cmd")&&0>j.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}return!1};g.prototype._receive_input=function(a,c){var b;if(this._prevent_capture)this._keys_down.length&&(this._keys_down=[]);else if(b=y(a.keyCode),(c||this._keys_down.length||!("alt"===b||b===m))&&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<= +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}return!1};g.prototype._receive_input=function(a,c){var b;if(this._prevent_capture)this._keys_down.length&&(this._keys_down=[]);else if(b=z(a.keyCode),(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=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))&& +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(null!=a.keys)return b(a);"string"===typeof a&&(a=a.split(" "));f=this._registered_combos;g=[];d=0;for(e=f.length;d< -e;d++)c=f[d],null!=c&&(c.is_unordered&&v(a,c.keys)||!c.is_unordered&&x(a,c.keys)?g.push(b(c)):g.push(void 0));return g};g.prototype.unregister_many=function(a){var c,b,d,e;e=[];b=0;for(d=a.length;be;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};F=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)};G=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};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,m),"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":")"};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", +on_keydown:c})};g.prototype.counting_combo=function(a,c){return this.register_combo({keys:a,is_counting:!0,is_unordered:!1,on_keydown:c})};g.prototype.sequence_combo=function(a,c){return this.register_combo({keys:a,on_keydown:c,is_sequence:!0})};g.prototype.register_combo=function(a){var c,b,d;"string"===typeof a.keys&&(a.keys=a.keys.split(" "));d=this._defaults;for(c in d)E.call(d,c)&&(b=d[c],void 0===a[c]&&(a[c]=b));a=new x(a);if(I(a))return this._registered_combos.push(a),a};g.prototype.register_many= +function(a){var c,b,d,e;e=[];b=0;for(d=a.length;bd;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;d< +e;d++)c=f[d],null!=c&&(c.is_unordered&&v(a,c.keys)||!c.is_unordered&&y(a,c.keys)?g.push(b(c)):g.push(void 0));return g};g.prototype.unregister_many=function(a){var c,b,d,e;e=[];b=0;for(d=a.length;be;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};q=function(){if(m.debug)return console.log.apply(console,arguments)};G=function(a){var c,b,d;c=!1;for(d in p)if(b=p[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||q("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&&q('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":")"};p={"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:"="};-1!==navigator.userAgent.indexOf("Mac OS X")&&(m="cmd");-1!==navigator.userAgent.indexOf("Opera")&&(r["17"]="cmd");"function"===typeof define&&define.amd?define([],function(){return o}):"undefined"!== -typeof exports&&null!==exports?exports.keypress=o:window.keypress=o}).call(this); +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=p;m._is_array_in_array_sorted=C;-1!==navigator.userAgent.indexOf("Mac OS X")&&(o="cmd");-1!==navigator.userAgent.indexOf("Opera")&&(p["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.coffee b/keypress.coffee index 3c4579e..31f7660 100644 --- a/keypress.coffee +++ b/keypress.coffee @@ -565,7 +565,7 @@ class keypress.Listener @_registered_combos.splice i, 1 break - if keys_or_combo.keys? + if keys_or_combo instanceof Combo unregister_combo keys_or_combo else if typeof keys_or_combo is "string" @@ -900,6 +900,10 @@ _keycode_dictionary = 61 : "-" 173 : "=" +# For testing only: +keypress._keycode_dictionary = _keycode_dictionary +keypress._is_array_in_array_sorted = _is_array_in_array_sorted + ############ # Initialize ############ diff --git a/keypress.js b/keypress.js index dbb5957..3a6b053 100644 --- a/keypress.js +++ b/keypress.js @@ -704,7 +704,7 @@ Combo options available and their defaults: return _results; }; })(this); - if (keys_or_combo.keys != null) { + if (keys_or_combo instanceof Combo) { return unregister_combo(keys_or_combo); } else { if (typeof keys_or_combo === "string") { @@ -1111,6 +1111,10 @@ Combo options available and their defaults: 173: "=" }; + keypress._keycode_dictionary = _keycode_dictionary; + + keypress._is_array_in_array_sorted = _is_array_in_array_sorted; + _decide_meta_key(); _change_keycodes_by_browser(); From 738acab339ee9db62e206360ffab1b724f6dafa8 Mon Sep 17 00:00:00 2001 From: David Mauro Date: Sun, 9 Nov 2014 14:30:00 -0500 Subject: [PATCH 12/12] Test fixes --- test/run_tests.html | 2 +- test/tests.coffee | 23 ++++++++++++++++++----- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/test/run_tests.html b/test/run_tests.html index 50c7cce..5c99671 100644 --- a/test/run_tests.html +++ b/test/run_tests.html @@ -8,7 +8,7 @@ - + diff --git a/test/tests.coffee b/test/tests.coffee index b67f3a6..011403c 100644 --- a/test/tests.coffee +++ b/test/tests.coffee @@ -7,7 +7,7 @@ describe "Keypress:", -> listener = new window.keypress.Listener() convert_readable_key_to_keycode = (keyname) -> - for keycode, name of window._keycode_dictionary + for keycode, name of window.keypress._keycode_dictionary return keycode if name is keyname return @@ -730,19 +730,19 @@ describe "Keypress Functional components:", -> describe "_is_array_in_array_sorted", -> it "case 1", -> - result = window._is_array_in_array_sorted ["a", "b"], ["a", "b", "c"] + result = window.keypress._is_array_in_array_sorted ["a", "b"], ["a", "b", "c"] expect(result).toBe(true) it "case 2", -> - result = window._is_array_in_array_sorted ["a", "b", "c"], ["a", "b"] + result = window.keypress._is_array_in_array_sorted ["a", "b", "c"], ["a", "b"] expect(result).toBe(false) it "case 3", -> - result = window._is_array_in_array_sorted ["a", "b"], ["a", "x", "b"] + result = window.keypress._is_array_in_array_sorted ["a", "b"], ["a", "x", "b"] expect(result).toBe(true) it "case 4", -> - result = window._is_array_in_array_sorted ["b", "a"], ["a", "x", "b"] + result = window.keypress._is_array_in_array_sorted ["b", "a"], ["a", "x", "b"] expect(result).toBe(false) describe "_fuzzy_match_combo_arrays", -> @@ -826,6 +826,7 @@ describe "APIs behave as expected:", -> ) count = listener.get_registered_combos().length expect(count).toEqual(1) + debugger listener.unregister_combo(["shift", "s"]) count = listener.get_registered_combos().length expect(count).toEqual(0) @@ -862,3 +863,15 @@ describe "APIs behave as expected:", -> listener.unregister_combo("shift s") count = listener.get_registered_combos().length expect(count).toEqual(0) + + it "unregisters a combo passed in directly", -> + debugger + combo = listener.register_combo( + keys : "shift s" + is_unordered : true + ) + count = listener.get_registered_combos().length + expect(count).toEqual(1) + listener.unregister_combo(combo) + count = listener.get_registered_combos().length + expect(count).toEqual(0)