diff --git a/coffee-compiler.sh b/coffee-compiler.sh deleted file mode 100755 index 5be5067..0000000 --- a/coffee-compiler.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/bash -VERSION="2.1.4" -YEAR="2016" -cd "$( cd "$( dirname "$0" )" && pwd )" -coffee -c keypress.coffee -java -jar compiler.jar --js keypress.js --js_output_file keypress-$VERSION.min.js -printf "/*\n Keypress version ${VERSION} (c) ${YEAR} David Mauro.\n Licensed under the Apache License, Version 2.0\n http://www.apache.org/licenses/LICENSE-2.0\n*/\n"|cat - keypress-$VERSION.min.js > /tmp/out && mv /tmp/out keypress-$VERSION.min.js diff --git a/compiler.jar b/compiler.jar deleted file mode 100755 index 52a920f..0000000 Binary files a/compiler.jar and /dev/null differ diff --git a/dist/keypress.min.js b/dist/keypress.min.js new file mode 100644 index 0000000..1bbeafc --- /dev/null +++ b/dist/keypress.min.js @@ -0,0 +1 @@ +!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.keypress=t():e.keypress=t()}(this,function(){return function(e){function t(n){if(s[n])return s[n].exports;var r=s[n]={i:n,l:!1,exports:{}};return e[n].call(r.exports,r,r.exports,t),r.l=!0,r.exports}var s={};return t.m=e,t.c=s,t.d=function(e,s,n){t.o(e,s)||Object.defineProperty(e,s,{configurable:!1,enumerable:!0,get:n})},t.n=function(e){var s=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(s,"a",s),s},t.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},t.p="",t(t.s=0)}([function(e,t,s){var n,r,i,o,_,c,u,l,a,h,f,d,y,m,p,k,v,g,b,w,q,x,j,O,E={}.hasOwnProperty,z=[].indexOf;h={is_unordered:!1,is_counting:!1,is_exclusive:!1,is_solitary:!1,prevent_default:!1,prevent_repeat:!1,normalize_caps_lock:!1},x=["meta","alt","option","ctrl","shift","cmd"],w="ctrl",(O={}).debug=!1,i=class{constructor(e){var t,s;for(t in e)E.call(e,t)&&!1!==(s=e[t])&&(this[t]=s);this.keys=this.keys||[],this.count=this.count||0}allows_key_repeat(){return!this.prevent_repeat&&"function"==typeof this.on_keydown}reset(){return this.count=0,this.keyup_fired=null}},O.Listener=class{constructor(e,t){var s,n,r;"undefined"!=typeof jQuery&&null!==jQuery&&e instanceof jQuery&&(1!==e.length&&b("Warning: your jQuery selector should have exactly one object."),e=e[0]),this.should_suppress_event_defaults=!1,this.should_force_event_defaults=!1,this.sequence_delay=800,this._registered_combos=[],this._keys_down=[],this._active_combos=[],this._sequence=[],this._sequence_timer=null,this._prevent_capture=!1,this._defaults=t||{};for(n in h)E.call(h,n)&&(r=h[n],this._defaults[n]=this._defaults[n]||r);this.element=e||document.body,s=function(e,t,s){return e.addEventListener?e.addEventListener(t,s):e.attachEvent&&e.attachEvent(`on${t}`,s),s},this.keydown_event=s(this.element,"keydown",e=>(e=e||window.event,this._receive_input(e,!0),this._bug_catcher(e))),this.keyup_event=s(this.element,"keyup",e=>(e=e||window.event,this._receive_input(e,!1))),this.blur_event=s(window,"blur",()=>{var e,t,s,n;for(t=0,s=(n=this._keys_down).length;t=0&&"cmd"!==(t=u(null!=(s=e.keyCode)?s:e.key))&&"shift"!==t&&"alt"!==t&&"caps"!==t&&"tab"!==t)return this._receive_input(e,!1)}_cmd_bug_check(e){return!("cmd"===w&&z.call(this._keys_down,"cmd")>=0&&z.call(e,"cmd")<0)}_prevent_default(e,t){if((t||this.should_suppress_event_defaults)&&!this.should_force_event_defaults&&(e.preventDefault?e.preventDefault():e.returnValue=!1,e.stopPropagation))return e.stopPropagation()}_get_active_combos(e){var t,s;return t=[],(s=f(this._keys_down,function(t){return t!==e})).push(e),this._match_combo_arrays(s,e=>{if(this._cmd_bug_check(e.keys))return t.push(e)}),this._fuzzy_match_combo_arrays(s,e=>{if(!(z.call(t,e)>=0))return!e.is_solitary&&this._cmd_bug_check(e.keys)?t.push(e):void 0}),t}_get_potential_combos(e){var t,s,n,r,i;for(r=[],s=0,n=(i=this._registered_combos).length;s=0&&this._cmd_bug_check(t.keys)&&r.push(t);return r}_add_to_active_combos(e){var t,s,n,r,i,o,_,c,u,l,a,h,f,d,y;if(y=!1,d=!0,r=!1,z.call(this._active_combos,e)>=0)return!0;if(this._active_combos.length)for(o=_=0,h=this._active_combos.length;0<=h?_h;o=0<=h?++_:--_)if((t=this._active_combos[o])&&t.is_exclusive&&e.is_exclusive){if(n=t.keys,!y)for(l=0,c=n.length;ln;t=0<=n?++s:--s)if(this._active_combos[t]===e){(e=this._active_combos.splice(t,1)[0]).reset();break}}_get_possible_sequences(){var e,t,s,n,r,i,o,_,c,u,l,a,h;for(_=[],n=0,r=(u=this._registered_combos).length;n=l;s=1<=l?++i:--i)if(h=this._sequence.slice(-s),e.is_sequence&&(!(z.call(e.keys,"shift")<0)||(h=f(h,function(e){return"shift"!==e})).length)){for(t=c=0,a=h.length;0<=a?ca;t=0<=a?++c:--c){if(e.keys[t]!==h[t]){o=!1;break}o=!0}o&&_.push(e)}return _}_add_key_to_sequence(e,t){var s,n,r,i;if(this._sequence.push(e),(i=this._get_possible_sequences()).length){for(n=0,r=i.length;n-1&&(this._sequence_timer=setTimeout(()=>this._sequence=[],this.sequence_delay))}else this._sequence=[]}_get_sequence(e){var t,s,n,r,i,o,_,c,u,l,a,h,d;for(r=0,i=(u=this._registered_combos).length;r=l;n=1<=l?++o:--o)if(d=f(this._sequence,function(e){return z.call(t.keys,"shift")>=0||"shift"!==e}).slice(-n),t.keys.length===d.length)for(s=c=0,a=d.length;0<=a?ca;s=0<=a?++c:--c)if(h=d[s],!(z.call(t.keys,"shift")<0&&"shift"===h||"shift"===e&&z.call(t.keys,"shift")<0)){if(t.keys[s]!==h){_=!1;break}_=!0}if(_)return t.is_exclusive&&(this._sequence=[]),t}return!1}_receive_input(e,t){var s,n;if(this._prevent_capture)this._keys_down.length&&(this._keys_down=[]);else if(s=u(null!=(n=e.keyCode)?n:e.key),(t||this._keys_down.length||"alt"!==s&&s!==w)&&s)return t?this._key_down(s,e):this._key_up(s,e)}_fire(e,t,s,n){if("function"==typeof t["on_"+e]&&this._prevent_default(s,!0!==t["on_"+e].call(t.this,s,t.count,n)),"release"===e&&(t.count=0),"keyup"===e)return t.keyup_fired=!0}_match_combo_arrays(e,t){var s,n,r,i,o;for(n=0,r=(i=this._registered_combos).length;n=0&&s.splice(s.indexOf("caps"),1),(!o.is_unordered&&c(s,o.keys)||o.is_unordered&&_(s,o.keys))&&t(o)}_fuzzy_match_combo_arrays(e,t){var s,n,r,i;for(s=0,n=(r=this._registered_combos).length;s=0){s=!0;break}return s}_key_down(e,t){var s,n,r,i,o,_,c,u,a,h,f,d,y,m,p;(p=l(e,t))&&(e=p),this._add_key_to_sequence(e,t),(m=this._get_sequence(e))&&this._fire("keydown",m,t);for(a in q)t[r=q[a]]&&(a===e||z.call(this._keys_down,a)>=0||this._keys_down.push(a));for(a in q)if(r=q[a],a!==e&&z.call(this._keys_down,a)>=0&&!t[r]){if("cmd"===a&&"cmd"!==w)continue;for(i=o=0,y=this._keys_down.length;0<=y?oy;i=0<=y?++o:--o)this._keys_down[i]===a&&this._keys_down.splice(i,1)}for(n=this._get_active_combos(e),d=this._get_potential_combos(e),u=0,_=n.length;u<_;u++)s=n[u],this._handle_combo_down(s,d,e,t);if(d.length)for(h=0,c=d.length;h=0&&(r=!0,!e.allows_key_repeat()))return!1;if(u=this._add_to_active_combos(e,s),e.keyup_fired=!1,i=!1,e.is_exclusive)for(o=0,_=t.length;o<_;o++)if((c=t[o]).is_exclusive&&c.keys.length>e.keys.length){i=!0;break}return!i&&(e.is_counting&&"function"==typeof e.on_keydown&&(e.count+=1),u)?this._fire("keydown",e,n,r):void 0}_key_up(e,t){var s,n,r,i,o,_,c,u,a,h,f,d,y,m,p,k,v,b,w;if(w=e,(b=l(e,t))&&(e=b),b=g[w],t.shiftKey?b&&z.call(this._keys_down,b)>=0||(e=w):w&&z.call(this._keys_down,w)>=0||(e=b),(v=this._get_sequence(e))&&this._fire("keyup",v,t),z.call(this._keys_down,e)<0)return!1;for(o=_=0,y=this._keys_down.length;0<=y?_y;o=0<=y?++_:--_)if((m=this._keys_down[o])===e||m===b||m===w){this._keys_down.splice(o,1);break}for(n=this._active_combos.length,i=[],h=0,c=(p=this._active_combos).length;h=0&&i.push(s);for(f=0,u=i.length;f1)for(d=0,a=(k=this._active_combos).length;d=0||this._keys_remain(s)||this._remove_from_active_combos(s)}_handle_combo_up(e,t,s){var n,r;this._prevent_default(t,e&&e.prevent_default),r=this._keys_remain(e),e.keyup_fired||((n=this._keys_down.slice()).push(s),e.is_solitary&&!_(n,e.keys)||(this._fire("keyup",e,t),e.is_counting&&"function"==typeof e.on_keyup&&"function"!=typeof e.on_keydown&&(e.count+=1))),r||(this._fire("release",e,t),this._remove_from_active_combos(e))}simple_combo(e,t){return this.register_combo({keys:e,on_keydown:t})}counting_combo(e,t){return this.register_combo({keys:e,is_counting:!0,is_unordered:!1,on_keydown:t})}sequence_combo(e,t){return this.register_combo({keys:e,on_keydown:t,is_sequence:!0,is_exclusive:!0})}register_combo(e){var t,s,n,r;"string"==typeof e.keys&&(e.keys=e.keys.split(" ")),n=this._defaults;for(s in n)E.call(n,s)&&(r=n[s],void 0===e[s]&&(e[s]=r));if(t=new i(e),j(t))return this._registered_combos.push(t),t}register_many(e){var t,s,n,r;for(r=[],s=0,n=e.length;s{var t,s,n,r;for(r=[],t=s=0,n=this._registered_combos.length;0<=n?sn;t=0<=n?++s:--s){if(e===this._registered_combos[t]){this._registered_combos.splice(t,1);break}r.push(void 0)}return r}),e instanceof i)return u(e);for("string"==typeof e&&(e=e.split(" ")),o=[],s=0,n=(r=this._registered_combos).length;s=0))return!1;return!0},c=function(e,t){var s,n,r;if(e.length!==t.length)return!1;for(s=n=0,r=e.length;0<=r?nr;s=0<=r?++n:--n)if(e[s]!==t[s])return!1;return!0},y=function(e,t){var s,n,r;for(n=0,r=e.length;n=r;s=0<=r?++n:--n)if(e[s]===t)return s;return-1},m=function(e,t){var s,n,r,i,o;for(o=0,r=0,i=e.length;r=o))return!1;o=s}return!0},b=function(){if(O.debug)return console.log(...arguments)},p=function(e){var t,s,n;s=!1;for(t in v)if(n=v[t],e===n){s=!0;break}if(!s)for(t in g)if(n=g[t],e===n){s=!0;break}return s},j=function(e){var t,s,n,r,i,o,_,c,u,l,a,f,y,m;for(m=!0,e.keys.length||b("You're trying to bind a combo with no keys:",e),s=r=0,f=e.keys.length;0<=f?rf;s=0<=f?++r:--r)n=e.keys[s],(t=k[n])&&(n=e.keys[s]=t),"meta"===n&&e.keys.splice(s,1,w),"cmd"===n&&b('Warning: use the "meta" key rather than "cmd" for Windows compatibility');for(_=0,i=(y=e.keys).length;_=0||z.call(e.keys,"cmd")>=0){for(l=e.keys.slice(),u=0,o=x.length;u-1&&l.splice(s,1);l.length>1&&(b("META and CMD key combos cannot have more than 1 non-modifier keys",e,l),m=!1)}for(a in e)e[a],"undefined"===h[a]&&b(`The property ${a} is not a valid combo property. Your combo has still been registered.`);return m},l=function(e,t){var s;return!!t.shiftKey&&(null!=(s=g[e])&&s)},q={cmd:"metaKey",ctrl:"ctrlKey",shift:"shiftKey",alt:"altKey"},k={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"},g={"/":"?",".":">",",":"<","'":'"',";":":","[":"{","]":"}","\\":"|","`":"~","=":"+","-":"_",1:"!",2:"@",3:"#",4:"$",5:"%",6:"^",7:"&",8:"*",9:"(",0:")"},v={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=v,O._is_array_in_array_sorted=m,a(),o(),n=[],void 0!==(r=function(){return O}.apply(t,n))&&(e.exports=r)}])}); \ No newline at end of file diff --git a/karma.conf.js b/karma.conf.js new file mode 100644 index 0000000..deed3e7 --- /dev/null +++ b/karma.conf.js @@ -0,0 +1,71 @@ +// Karma configuration +// Generated on Sun Sep 24 2017 18:29:39 GMT+0200 (CEST) + +module.exports = function(config) { + config.set({ + + // base path that will be used to resolve all patterns (eg. files, exclude) + basePath: '', + + + // frameworks to use + // available frameworks: https://npmjs.org/browse/keyword/karma-adapter + frameworks: ['jasmine'], + + + // list of files / patterns to load in the browser + files: [ + 'dist/keypress.min.js', + 'test/*.coffee' + ], + + + // list of files to exclude + exclude: [ + ], + + + // preprocess matching files before serving them to the browser + // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor + preprocessors: { + '**/*.coffee': ['coffee'] + }, + + + // test results reporter to use + // possible values: 'dots', 'progress' + // available reporters: https://npmjs.org/browse/keyword/karma-reporter + reporters: ['progress'], + + + // web server port + port: 9876, + + + // enable / disable colors in the output (reporters and logs) + colors: true, + + + // level of logging + // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG + logLevel: config.LOG_DEBUG, + + + // enable / disable watching file and executing tests whenever any file changes + autoWatch: true, + + + // start these browsers + // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher + browsers: ['Chrome'], + + + // Continuous Integration mode + // if true, Karma captures browsers, runs the tests and exits + singleRun: false, + + // Concurrency level + // how many browser should be started simultaneous + concurrency: Infinity + }) +} diff --git a/keypress-2.1.4.min.js b/keypress-2.1.4.min.js deleted file mode 100644 index 8bff56b..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 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); diff --git a/keypress.coffee b/keypress.coffee index 135d104..f4a1891 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"] @@ -291,7 +293,7 @@ class keypress.Listener # If we're working towards one, give them more time to keep going clearTimeout(@_sequence_timer) if @_sequence_timer if @sequence_delay > -1 - @_sequence_timer = setTimeout -> + @_sequence_timer = setTimeout => @_sequence = [] , @sequence_delay else @@ -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 diff --git a/keypress.js b/keypress.js deleted file mode 100644 index 8913d02..0000000 --- a/keypress.js +++ /dev/null @@ -1,1142 +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. - 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 - }; - - _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 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))) { - 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); diff --git a/package.json b/package.json index c2153da..b1e958e 100644 --- a/package.json +++ b/package.json @@ -1,15 +1,30 @@ { - "name" : "keypress", - "version" : "2.1.4", - "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": "@kba/keypress.js", + "version": "2.1.4-webpack-1", + "main": "dist/keypress.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": { + "coffee-loader": "^0.8.0", + "coffeescript": "^2.0.0", + "jasmine-core": "2.8.0", + "karma-chrome-launcher": "^2.2.0", + "karma-coffee-preprocessor": "^1.0.1", + "karma-jasmine": "^1.1.0", + "uglifyjs-webpack-plugin": "1.0.0-beta.2", + "webpack": "^3.6.0" + }, + "scripts": { + "prepublish": "webpack", + "test": "karma --single-run" }, "spm": { "main": "keypress.js", "ignore": [ "compiler.jar" ] + }, + "dependencies": { + "karma": "^1.7.1", + "karma-cli": "^1.0.1" } } diff --git a/test/run_tests.html b/test/run_tests.html deleted file mode 100644 index 5c99671..0000000 --- a/test/run_tests.html +++ /dev/null @@ -1,48 +0,0 @@ - - - - Keypress Jasmine tests - - - - - - - - - - - - - - - - - diff --git a/test/run_tests.sh b/test/run_tests.sh deleted file mode 100755 index b65ce4a..0000000 --- a/test/run_tests.sh +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/bash -cd "$( cd "$( dirname "$0" )" && pwd )" -cd .. -coffee -o . -cb ./keypress.coffee -coffee -c test/tests.coffee -open test/run_tests.html diff --git a/webpack.config.js b/webpack.config.js new file mode 100644 index 0000000..4665c79 --- /dev/null +++ b/webpack.config.js @@ -0,0 +1,23 @@ +const UglifyJSPlugin = require('uglifyjs-webpack-plugin') + +module.exports = { + entry: "./keypress.coffee", + output: { + path: __dirname + "/dist", + filename: "keypress.min.js", + libraryTarget: "umd", + library: "keypress" + }, + module: { + loaders: [ + {test: /\.coffee$/, loader: "coffee-loader"}, + ], + }, + plugins: [ + new UglifyJSPlugin({ + uglifyOptions: { + ecma: 6 + } + }) + ] +}