forked from dmauro/Keypress
-
Notifications
You must be signed in to change notification settings - Fork 1
/
keypress.js
21 lines (21 loc) · 9.88 KB
/
keypress.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/* Keypress version 1.0.8 */
(function(){var w,x,j,Q,R,S,T,y,z,A,U,G,H,m,V,W,X,Y,I,Z,$,aa,ba,ca,J,da,r,s,g,K,t,ea,L,u,M,B,v,N,C,h,O,D,p,E,P,F,fa,i=[].indexOf||function(a){for(var c=0,b=this.length;c<b;c++)if(c in this&&this[c]===a)return c;return-1},ga={}.hasOwnProperty;Array.prototype.filter||(Array.prototype.filter=function(a){var c,b,d,e;e=[];b=0;for(d=this.length;b<d;b++)c=this[b],a(c)&&e.push(c);return e});h=[];p=[];E=null;g=[];j=[];B=!1;L="ctrl";M="meta alt option ctrl shift cmd".split(" ");F=[];z={keys:[],count:0};t=function(){return console.log.apply(console,
arguments)};A=function(a,c){var b,d,e;if(a.length!==c.length)return false;d=0;for(e=a.length;d<e;d++){b=a[d];if(!(i.call(c,b)>=0))return false}return true};U=function(a,c){var b,d,e;if(a.length!==c.length)return false;b=d=0;for(e=a.length;0<=e?d<e:d>e;b=0<=e?++d:--d)if(a[b]!==c[b])return false;return true};aa=function(a,c){var b,d,e;d=0;for(e=a.length;d<e;d++){b=a[d];if(i.call(c,b)<0)return false}return true};ba=function(a,c){var b,d,e,f;e=d=0;for(f=a.length;e<f;e++){b=a[e];b=c.indexOf(b);if(b>=d)d=
b;else return false}return true};v=function(a,c){if((c||keypress.suppress_event_defaults)&&!keypress.force_event_defaults){a.preventDefault?a.preventDefault():a.returnValue=false;if(a.stopPropagation)return a.stopPropagation()}};S=function(a){if(a.prevent_repeat)return false;if(typeof a.on_keydown==="function")return true};K=function(a){var c,b,d,e;e=a.keys;b=0;for(d=e.length;b<d;b++){a=e[b];if(i.call(g,a)>=0){c=true;break}}return c};m=function(a,c,b){typeof c["on_"+a]==="function"&&v(b,c["on_"+a].call(c["this"],
b,c.count)===false);if(a==="release")c.count=0;if(a==="keyup")return c.keyup_fired=true};ea=function(a,c){var b,d,e;d=0;for(e=h.length;d<e;d++){b=h[d];(b.is_ordered&&U(a,b.keys)||!b.is_ordered&&A(a,b.keys))&&c(b)}};V=function(a,c){var b,d,e;d=0;for(e=h.length;d<e;d++){b=h[d];(b.is_ordered&&ba(b.keys,a)||!b.is_ordered&&aa(b.keys,a))&&c(b)}};y=function(a){return i.call(g,"cmd")>=0&&i.call(a,"cmd")<0?false:true};W=function(a){var c,b;c=[];b=g.filter(function(b){return b!==a});b.push(a);ea(b,function(a){if(y(a.keys))return c.push(a)});
V(b,function(a){if(!(i.call(c,a)>=0)&&!a.is_solitary&&y(a.keys))return c.push(a)});return c};Y=function(a){var c,b,d,e;b=[];d=0;for(e=h.length;d<e;d++){c=h[d];c.is_sequence||i.call(c.keys,a)>=0&&y(c.keys)&&b.push(c)}return b};R=function(a){var c,b,d,e,f,q,g,h,l,k,o;q=false;f=true;d=false;if(i.call(j,a)>=0)return true;if(j.length){e=g=0;for(k=j.length;0<=k?g<k:g>k;e=0<=k?++g:--g)if((c=j[e])&&c.is_exclusive&&a.is_exclusive){c=c.keys;if(!q){h=0;for(l=c.length;h<l;h++){b=c[h];q=true;if(i.call(a.keys,
b)<0){q=false;break}}}if(f&&!q){o=a.keys;h=0;for(l=o.length;h<l;h++){b=o[h];f=false;if(i.call(c,b)<0){f=true;break}}}if(q){if(d)D(j.splice(e,1));else{D(j.splice(e,1,a));d=true}f=false}}}f&&j.unshift(a);return q||f};O=function(a){var c,b,d,e;b=d=0;for(e=j.length;0<=e?d<e:d>e;b=0<=e?++d:--d){c=j[b];if(c===a){D(j.splice(b,1));break}}};D=function(a){if(a){a.count=null;return a.keyup_fired=null}};Q=function(a,c){var b,d,e,f;p.push(a);d=X();if(d.length){e=0;for(f=d.length;e<f;e++){b=d[e];v(c,b.prevent_default)}E&&
clearTimeout(E);keypress.sequence_delay>-1&&(E=setTimeout(function(){return p=[]},keypress.sequence_delay))}else p=[]};X=function(){var a,c,b,d,e,f,g,n,j,l,k;d=[];f=0;for(j=h.length;f<j;f++){a=h[f];c=g=1;for(l=p.length;1<=l?g<=l:g>=l;c=1<=l?++g:--g){e=p.slice(-c);if(a.is_sequence){if(i.call(a.keys,"shift")<0){e=e.filter(function(a){return a!=="shift"});if(!e.length)continue}c=n=0;for(k=e.length;0<=k?n<k:n>k;c=0<=k?++n:--n)if(a.keys[c]===e[c])b=true;else{b=false;break}b&&d.push(a)}}}return d};I=function(a){var c,
b,d,e,f,g,j,m,l,k,o;g=0;for(l=h.length;g<l;g++){c=h[g];if(c.is_sequence){b=j=1;for(k=p.length;1<=k?j<=k:j>=k;b=1<=k?++j:--j){f=p.filter(function(a){return i.call(c.keys,"shift")>=0?true:a!=="shift"}).slice(-b);if(c.keys.length===f.length){b=m=0;for(o=f.length;0<=o?m<o:m>o;b=0<=o?++m:--m){e=f[b];if(!(i.call(c.keys,"shift")<0&&e==="shift")&&!(a==="shift"&&i.call(c.keys,"shift")<0))if(c.keys[b]===e)d=true;else{d=false;break}}}}if(d)return c}}return false};H=function(a,c){var b;if(!c.shiftKey)return false;
b=s[a];return b!=null?b:false};Z=function(a,c,b){if(i.call(a.keys,c)<0)return false;v(b,a&&a.prevent_default);if(i.call(g,c)>=0&&!S(a))return false;c=R(a,c);a.keyup_fired=false;if(a.is_counting&&typeof a.on_keydown==="function")a.count=a.count+1;if(c)return m("keydown",a,b)};ca=function(a,c){var b,d,e,f;(d=H(a,c))&&(a=d);Q(a,c);(d=I(a))&&m("keydown",d,c);for(b in u){d=u[b];c[d]&&(b===a||i.call(g,b)>=0||g.push(b))}for(b in u){d=u[b];if(b!==a&&i.call(g,b)>=0&&!c[d]){d=e=0;for(f=g.length;0<=f?e<f:e>
f;d=0<=f?++e:--e)g[d]===b&&g.splice(d,1)}}d=W(a);e=0;for(f=d.length;e<f;e++){b=d[e];Z(b,a,c)}d=Y(a);if(d.length){e=0;for(f=d.length;e<f;e++){b=d[e];v(c,b.prevent_default)}}i.call(g,a)<0&&g.push(a)};$=function(a,c,b){var d,e;e=K(a);if(!a.keyup_fired){d=g.slice();d.push(b);if(!a.is_solitary||A(d,a.keys)){m("keyup",a,c);if(a.is_counting&&typeof a.on_keyup==="function"&&typeof a.on_keydown!=="function")a.count=a.count+1}}if(!e){m("release",a,c);O(a)}};J=function(a,c){var b,d,e,f,h,n;d=a;(e=H(a,c))&&(a=
e);e=s[d];c.shiftKey?e&&i.call(g,e)>=0||(a=d):d&&i.call(g,d)>=0||(a=e);(f=I(a))&&m("keyup",f,c);if(i.call(g,a)<0)return false;f=h=0;for(n=g.length;0<=n?h<n:h>n;f=0<=n?++h:--h)if((b=g[f])===a||b===e||b===d){g.splice(f,1);break}d=j.length;e=[];f=0;for(h=j.length;f<h;f++){b=j[f];i.call(b.keys,a)>=0&&e.push(b)}f=0;for(h=e.length;f<h;f++){b=e[f];$(b,c,a)}if(d>1){d=0;for(f=j.length;d<f;d++){b=j[d];b===void 0||i.call(e,b)>=0||K(b)||O(b)}}};C=function(a,c){var b;if(B)g.length&&(g=[]);else if(c||g.length)if(b=
G(a.keyCode))return c?ca(b,a):J(b,a)};P=function(a){var c,b,d,e;e=[];c=b=0;for(d=h.length;0<=d?b<d:b>d;c=0<=d?++b:--b)if(a===h[c]){h.splice(c,1);break}else e.push(void 0);return e};fa=function(a){var c,b,d,e,f;a.keys.length||t("You're trying to bind a combo with no keys.");b=e=0;for(f=a.keys.length;0<=f?e<f:e>f;b=0<=f?++e:--e){d=a.keys[b];(c=da[d])&&(d=a.keys[b]=c);d==="meta"&&a.keys.splice(b,1,L);d==="cmd"&&t('Warning: use the "meta" key rather than "cmd" for Windows compatibility')}f=a.keys;c=0;
for(e=f.length;c<e;c++){d=f[c];if(i.call(F,d)<0){t('Do not recognize the key "'+d+'"');return false}}if(i.call(a.keys,"meta")>=0||i.call(a.keys,"cmd")>=0){c=a.keys.slice();e=0;for(f=M.length;e<f;e++){d=M[e];(b=c.indexOf(d))>-1&&c.splice(b,1)}c.length>1&&t("META and CMD key combos cannot have more than 1 non-modifier keys",a,c)}return true};T=function(a){var c;if(i.call(g,"cmd")>=0&&(c=G(a.keyCode))!=="cmd"&&c!=="shift"&&c!=="alt"&&c!=="caps"&&c!=="tab")return C(a,false)};window.keypress={};keypress.force_event_defaults=
!1;keypress.suppress_event_defaults=!1;keypress.sequence_delay=800;keypress.get_registered_combos=function(){return h};keypress.reset=function(){h=[]};keypress.combo=function(a,c,b){b==null&&(b=false);return keypress.register_combo({keys:a,on_keydown:c,prevent_default:b})};keypress.counting_combo=function(a,c,b){b==null&&(b=false);return keypress.register_combo({keys:a,is_counting:true,is_ordered:true,on_keydown:c,prevent_default:b})};keypress.sequence_combo=function(a,c,b){b==null&&(b=false);return keypress.register_combo({keys:a,
on_keydown:c,is_sequence:true,prevent_default:b})};keypress.register_combo=function(a){var c,b;if(typeof a.keys==="string")a.keys=a.keys.split(" ");for(c in z)if(ga.call(z,c)){b=z[c];a[c]==null&&(a[c]=b)}if(fa(a)){h.push(a);return true}};keypress.register_many=function(a){var c,b,d,e;e=[];b=0;for(d=a.length;b<d;b++){c=a[b];e.push(keypress.register_combo(c))}return e};keypress.unregister_combo=function(a){var c,b,d;if(!a)return false;if(a.keys)return P(a);d=[];c=0;for(b=h.length;c<b;c++)(a=h[c])&&
(A(keys,a.keys)?d.push(P(a)):d.push(void 0));return d};keypress.unregister_many=function(a){var c,b,d,e;e=[];b=0;for(d=a.length;b<d;b++){c=a[b];e.push(keypress.unregister_combo(c))}return e};keypress.listen=function(){return B=false};keypress.stop_listening=function(){return B=true};G=function(a){return r[a]};u={cmd:"metaKey",ctrl:"ctrlKey",shift:"shiftKey",alt:"altKey"};da={control:"ctrl",command:"cmd","break":"pause",windows:"cmd",option:"alt",caps_lock:"caps",apostrophe:"'",semicolon:";",tilde:"~",
accent:"`",scroll_lock:"scroll",num_lock:"num"};s={"/":"?",".":">",",":"<","'":'"',";":":","[":"{","]":"}","\\":"|","`":"~","=":"+","-":"_",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:"escape",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",57392:"ctrl",63289:"num"};for(x in r)w=r[x],F.push(w);for(x in s)w=s[x],F.push(w);-1!==navigator.userAgent.indexOf("Mac OS X")&&(L="cmd");-1!==navigator.userAgent.indexOf("Opera")&&(r["17"]="cmd");N=function(a){return(document.attachEvent?document.readyState==="complete":document.readyState!=="loading")?a():setTimeout(function(){return N(a)},9)};N(function(){var a;a=function(a,b,d){if(a.addEventListener)return a.addEventListener(b,
d);if(a.attachEvent)return a.attachEvent("on"+b,d)};a(document.body,"keydown",function(a){a=a||window.event;C(a,true);return T(a)});a(document.body,"keyup",function(a){a=a||window.event;return C(a,false)});return a(window,"blur",function(){var a,b,d;b=0;for(d=g.length;b<d;b++){a=g[b];J(a,{})}g=[];return[]})})}).call(this);