Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save derek-adair/0ca684c495c9ef50e87ad7d240032095 to your computer and use it in GitHub Desktop.
Save derek-adair/0ca684c495c9ef50e87ad7d240032095 to your computer and use it in GitHub Desktop.
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e=e||self).hotkeys=t()}(this,function(){"use strict";var e="undefined"!=typeof navigator&&0<navigator.userAgent.toLowerCase().indexOf("firefox");function t(e,t,n){e.addEventListener?e.addEventListener(t,n,!1):e.attachEvent&&e.attachEvent("on".concat(t),function(){n(window.event)})}function n(e,t){for(var n=t.slice(0,t.length-1),o=0;o<n.length;o++)n[o]=e[n[o].toLowerCase()];return n}function o(e){"string"!=typeof e&&(e="");for(var t=(e=e.replace(/\s/g,"")).split(","),n=t.lastIndexOf("");0<=n;)t[n-1]+=",",t.splice(n,1),n=t.lastIndexOf("");return t}for(var r={backspace:8,tab:9,clear:12,enter:13,return:13,esc:27,escape:27,space:32,left:37,up:38,right:39,down:40,del:46,delete:46,ins:45,insert:45,home:36,end:35,pageup:33,pagedown:34,capslock:20,"⇪":20,",":188,".":190,"/":191,"`":192,"-":e?173:189,"=":e?61:187,";":e?59:186,"'":222,"[":219,"]":221,"\\":220},i={"⇧":16,shift:16,"⌥":18,alt:18,option:18,"⌃":17,ctrl:17,control:17,"⌘":91,cmd:91,command:91},a={16:"shiftKey",18:"altKey",17:"ctrlKey",91:"metaKey",shiftKey:16,ctrlKey:17,altKey:18,metaKey:91},f={16:!1,18:!1,17:!1,91:!1},c={},s=1;s<20;s++)r["f".concat(s)]=111+s;var l=[],p="all",y=[],d=function(e){return r[e.toLowerCase()]||i[e.toLowerCase()]||e.toUpperCase().charCodeAt(0)};function u(e){p=e||"all"}function h(){return p||"all"}function v(e){var t=e.scope,r=e.method,a=e.splitKey,f=void 0===a?"+":a;o(e.key).forEach(function(e){var o=e.split(f),a=o.length,s=o[a-1],l="*"===s?"*":d(s);if(c[l]){t=t||h();var p=1<a?n(i,o):[];c[l]=c[l].map(function(e){return r&&e.method!==r||e.scope!==t||!function(e,t){for(var n=e.length<t.length?t:e,o=e.length<t.length?e:t,r=!0,i=0;i<n.length;i++)~o.indexOf(n[i])||(r=!1);return r}(e.mods,p)?e:{}})}})}function g(e,t,n){var o;if(t.scope===n||"all"===t.scope){for(var r in o=0<t.mods.length,f)Object.prototype.hasOwnProperty.call(f,r)&&(!f[r]&&~t.mods.indexOf(+r)||f[r]&&!~t.mods.indexOf(+r))&&(o=!1);(0!==t.mods.length||f[16]||f[18]||f[17]||f[91])&&!o&&"*"!==t.shortcut||!1===t.method(e,t)&&(e.preventDefault?e.preventDefault():e.returnValue=!1,e.stopPropagation&&e.stopPropagation(),e.cancelBubble&&(e.cancelBubble=!0))}}function w(e){var t=c["*"],n=e.keyCode||e.which||e.charCode;if(k.filter.call(this,e)){if(93!==n&&224!==n||(n=91),~l.indexOf(n)||229===n||l.push(n),["ctrlKey","altKey","shiftKey","metaKey"].forEach(function(t){var n=a[t];e[t]&&!~l.indexOf(n)?l.push(n):!e[t]&&~l.indexOf(n)&&l.splice(l.indexOf(n),1)}),n in f){for(var o in f[n]=!0,i)i[o]===n&&(k[o]=!0);if(!t)return}for(var r in f)Object.prototype.hasOwnProperty.call(f,r)&&(f[r]=e[a[r]]);e.getModifierState&&(!e.altKey||e.ctrlKey)&&e.getModifierState("AltGraph")&&(~l.indexOf(17)||l.push(17),~l.indexOf(18)||l.push(18),f[17]=!0,f[18]=!0);var s=h();if(t)for(var p=0;p<t.length;p++)t[p].scope===s&&("keydown"===e.type&&t[p].keydown||"keyup"===e.type&&t[p].keyup)&&g(e,t[p],s);if(n in c)for(var y=0;y<c[n].length;y++)if(("keydown"===e.type&&c[n][y].keydown||"keyup"===e.type&&c[n][y].keyup)&&c[n][y].key){for(var u=c[n][y],v=u.key.split(u.splitKey),w=[],m=0;m<v.length;m++)w.push(d(v[m]));w.sort().join("")===l.sort().join("")&&g(e,u,s)}}}function k(e,r,a){l=[];var s=o(e),p=[],u="all",h=document,v=0,g=!1,m=!0,O="+";for(void 0===a&&"function"==typeof r&&(a=r),"[object Object]"===Object.prototype.toString.call(r)&&(r.scope&&(u=r.scope),r.element&&(h=r.element),r.keyup&&(g=r.keyup),void 0!==r.keydown&&(m=r.keydown),"string"==typeof r.splitKey&&(O=r.splitKey)),"string"==typeof r&&(u=r);v<s.length;v++)p=[],1<(e=s[v].split(O)).length&&(p=n(i,e)),(e="*"===(e=e[e.length-1])?"*":d(e))in c||(c[e]=[]),c[e].push({keyup:g,keydown:m,scope:u,mods:p,shortcut:s[v],method:a,key:s[v],splitKey:O});void 0!==h&&!~y.indexOf(h)&&window&&(y.push(h),t(h,"keydown",function(e){w(e)}),t(window,"focus",function(){l=[]}),t(h,"keyup",function(e){w(e),function(e){var t=e.keyCode||e.which||e.charCode,n=l.indexOf(t);if(n<0||l.splice(n,1),e.key&&"meta"==e.key.toLowerCase()&&l.splice(0,l.length),93!==t&&224!==t||(t=91),t in f)for(var o in f[t]=!1,i)i[o]===t&&(k[o]=!1)}(e)}))}var m={setScope:u,getScope:h,deleteScope:function(e,t){var n,o;for(var r in e=e||h(),c)if(Object.prototype.hasOwnProperty.call(c,r))for(n=c[r],o=0;o<n.length;)n[o].scope===e?n.splice(o,1):o++;h()===e&&u(t||"all")},getPressedKeyCodes:function(){return l.slice(0)},isPressed:function(e){return"string"==typeof e&&(e=d(e)),!!~l.indexOf(e)},filter:function(e){var t=e.target||e.srcElement,n=t.tagName,o=!0;return!t.isContentEditable&&("INPUT"!==n&&"TEXTAREA"!==n||t.readOnly)||(o=!1),o},unbind:function(e){if(e){if(Array.isArray(e))e.forEach(function(e){e.key&&v(e)});else if("object"==typeof e)e.key&&v(e);else if("string"==typeof e){for(var t=arguments.length,n=Array(1<t?t-1:0),o=1;o<t;o++)n[o-1]=arguments[o];var r=n[0],i=n[1];"function"==typeof r&&(i=r,r=""),v({key:e,scope:r,method:i,splitKey:"+"})}}else Object.keys(c).forEach(function(e){return delete c[e]})}};for(var O in m)Object.prototype.hasOwnProperty.call(m,O)&&(k[O]=m[O]);if("undefined"!=typeof window){var K=window.hotkeys;k.noConflict=function(e){return e&&window.hotkeys===k&&(window.hotkeys=K),k},window.hotkeys=k}return k}),hotkeys("ctrl+a",function(e,t){switch(t.key){case"ctrl+a":alert("STOP BEING A BITCH AND BUILD THIS THING")}});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment