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