forked from ccampbell/mousetrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mousetrap.min.js
6 lines (6 loc) · 2.51 KB
/
mousetrap.min.js
1
2
3
4
5
6
/* mousetrap v1.0 craig.is/killing/mice */
window.Mousetrap=function(){function m(a,e,b){if(a.addEventListener)return a.addEventListener(e,b,!1);a.attachEvent("on"+e,b)}function n(a){var a=a||{},e=!1,b;for(b in h)a[b]?e=!0:h[b]=0;e||(k=!1)}function q(a,e,b,c){var f,g,d=[];if(!i[a])return[];if("keyup"==b&&(15<a&&19>a||91==a))e=[a];for(f=0;f<i[a].length;++f)g=i[a][f],!(g.seq&&h[g.seq]!=g.level)&&(b==g.action&&e.sort().join(",")===g.modifiers.sort().join(","))&&(c&&i[a].splice(f,1),d.push(g));return d}function r(a,e,b){var c;c=b.target||b.srcElement;
var f=c.tagName;c=-1<(" "+c.className+" ").indexOf(" mousetrap ")?!1:"INPUT"==f||"SELECT"==f||"TEXTAREA"==f;if(!c){c=[];b.shiftKey&&c.push(j.shift);b.altKey&&c.push(j.alt);b.ctrlKey&&c.push(j.ctrl);b.metaKey&&c.push(j.command);c=q(a,c,e);for(var g={},d=!1,f=0;f<c.length;++f)if(c[f].seq)d=!0,g[c[f].seq]=1,c[f].callback(b);else if(!d&&!k){c[f].callback(b);break}e==k&&!(15<a&&19>a||91==a)&&n(g)}}function v(a){r(93==a.keyCode||224==a.keyCode?91:a.keyCode,"keydown",a)}function w(a){o===a.keyCode?o=!1:
r(93==a.keyCode||224==a.keyCode?91:a.keyCode,"keyup",a)}function x(a,e,b,c){h[a]=0;var f=function(){k=c;++h[a];clearTimeout(s);s=setTimeout(n,1E3)},g=function(a){b(a);"keydown"===c&&(o=a.keyCode);setTimeout(n,10)},d;for(d=0;d<e.length;++d)t(e[d],d<e.length-1?f:g,c,a,d)}function t(a,e,b,c,f){var a=a.replace(/\s+/g," "),g=a.split(" "),d,h=[];if(1<g.length)return x(a,g,e,b);g="+"===a?["+"]:a.split("+");for(a=0;a<g.length;++a)d=g[a],u[d]&&(h.push(j.shift),d=u[d]),d=j[d]||d.toUpperCase().charCodeAt(0),
(15<d&&19>d||91==d)&&h.push(d);i[d]||(i[d]=[]);q(d,h,b,!c);i[d][c?"unshift":"push"]({callback:e,modifiers:h,action:b,seq:c,level:f})}for(var j={backspace:8,tab:9,enter:13,"return":13,shift:16,ctrl:17,alt:18,option:18,capslock:20,esc:27,escape:27,space:32,pageup:33,pagedown:34,end:35,home:36,left:37,up:38,right:39,down:40,del:46,meta:91,command:91,";":186,"=":187,",":188,"-":189,".":190,"/":191,"`":192,"[":219,"\\":220,"]":221,"'":222},u={"~":"`","!":"1","@":"2","#":"3",$:"4","%":"5","^":"6","&":"7",
"*":"8","(":"9",")":"0",_:"-","+":"=",":":";",'"':"'","<":",",">":".","?":"/","|":"\\"},i={},p={},h={},s,o=!1,k=!1,l=1;20>l;++l)j["f"+l]=111+l;return{bind:function(a,e,b){for(var b=b||"keydown",c=a instanceof Array?a:a.split(","),f=b,g=0;g<c.length;++g)t(c[g],e,f);p[a+":"+b]=e},trigger:function(a,e){p[a+":"+(e||"keydown")]()},addEvent:function(a,e,b){m(a,e,b)},reset:function(){i={};p={}},init:function(){m(document,"keydown",v);m(document,"keyup",w)}}}();Mousetrap.addEvent(window,"load",Mousetrap.init);