UNPKG

hotkeys-js

Version:

A simple micro-library for defining and dispatching keyboard shortcuts. It has no dependencies.

11 lines (10 loc) 8.22 kB
var hotkeys=function(){"use strict"; /*! * hotkeys-js v4.0.5 * A simple micro-library for defining and dispatching keyboard shortcuts. It has no dependencies. * * @author kenny wong <wowohoo@qq.com> * @license MIT * @homepage https://jaywcjlove.github.io/hotkeys-js */const e="undefined"!=typeof navigator&&navigator.userAgent.toLowerCase().indexOf("firefox")>0;function t(e,t,n,o){e.addEventListener?e.addEventListener(t,n,o):e.attachEvent&&e.attachEvent(`on${t}`,n)}function n(e,t,n,o){e&&(e.removeEventListener?e.removeEventListener(t,n,o):e.detachEvent&&e.detachEvent(`on${t}`,n))}function o(e,t){const n=t.slice(0,t.length-1),o=[];for(let t=0;t<n.length;t++)o.push(e[n[t].toLowerCase()]);return o}function r(e){"string"!=typeof e&&(e="");const t=(e=e.replace(/\s/g,"")).split(",");let n=t.lastIndexOf("");for(;n>=0;)t[n-1]+=",",t.splice(n,1),n=t.lastIndexOf("");return t}function s(e,t,n){let o=e.keyCode||e.which||e.charCode;if(e.key){const o=e.key.toLowerCase();if(o in t)return t[o];if(o in n)return n[o];if(/^[a-z]$/i.test(e.key))return e.key.toUpperCase().charCodeAt(0)}return e.code&&/^Key[A-Z]$/.test(e.code)&&(o=e.code.charCodeAt(3)),o}const c={backspace:8,"⌫":8,tab:9,clear:12,enter:13,"↩":13,return:13,esc:27,escape:27,space:32,left:37,up:38,right:39,down:40,arrowup:38,arrowdown:40,arrowleft:37,arrowright:39,del:46,delete:46,ins:45,insert:45,home:36,end:35,pageup:33,pagedown:34,capslock:20,num_0:96,num_1:97,num_2:98,num_3:99,num_4:100,num_5:101,num_6:102,num_7:103,num_8:104,num_9:105,num_multiply:106,num_add:107,num_enter:108,num_subtract:109,num_decimal:110,num_divide:111,"⇪":20,",":188,".":190,"/":191,"`":192,"-":e?173:189,"=":e?61:187,";":e?59:186,"'":222,"{":219,"}":221,"[":219,"]":221,"\\":220},i={"⇧":16,shift:16,"⌥":18,alt:18,option:18,"⌃":17,ctrl:17,control:17,"⌘":91,cmd:91,meta:91,command:91},l={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},a={};for(let e=1;e<20;e++)c[`f${e}`]=111+e;let d=[],u=null,p=null,y="all";const h=new Map,k=e=>c[e.toLowerCase()]||i[e.toLowerCase()]||e.toUpperCase().charCodeAt(0),m=e=>{y=e||"all"},w=()=>y||"all",g=e=>{const t=e.target||e.srcElement,{tagName:n}=t;let o=!0;const r="INPUT"===n&&!["checkbox","radio","range","button","file","reset","submit","color"].includes(t.type);return(t.isContentEditable||(r||"TEXTAREA"===n||"SELECT"===n)&&!t.readOnly)&&(o=!1),o};const O=(e,...t)=>{if(void 0===e)Object.keys(a).forEach((e=>{Array.isArray(a[e])&&a[e].forEach((e=>b(e))),delete a[e]})),L(null);else if(Array.isArray(e))e.forEach((e=>{e.key&&b(e)}));else if("object"==typeof e)e.key&&b(e);else if("string"==typeof e){let[n,o]=t;"function"==typeof n&&(o=n,n=""),b({key:e,scope:n,method:o,splitKey:"+"})}},b=({key:e,scope:t,method:n,splitKey:s="+"})=>{r(e).forEach((e=>{const r=e.split(s),c=r.length,l=r[c-1],f="*"===l?"*":k(l);if(!a[f])return;t||(t=w());const d=c>1?o(i,r):[],u=[];a[f]=a[f].filter((e=>{const o=(!n||e.method===n)&&e.scope===t&&function(e,t){const n=e.length>=t.length?e:t,o=e.length>=t.length?t:e;let r=!0;for(let e=0;e<n.length;e++)-1===o.indexOf(n[e])&&(r=!1);return r}(e.mods,d);return o&&u.push(e.element),!o})),u.forEach((e=>L(e)))}))};function v(e,t,n,o){if(t.element!==o)return;let r;if(t.scope===n||"all"===t.scope){r=t.mods.length>0;for(const e in f)Object.prototype.hasOwnProperty.call(f,e)&&(!f[e]&&t.mods.indexOf(+e)>-1||f[e]&&-1===t.mods.indexOf(+e))&&(r=!1);(0!==t.mods.length||f[16]||f[18]||f[17]||f[91])&&!r&&"*"!==t.shortcut||(t.keys=[],t.keys=t.keys.concat(d),!1===t.method(e,t)&&(e.preventDefault?e.preventDefault():e.returnValue=!1,e.stopPropagation&&e.stopPropagation(),e.cancelBubble&&(e.cancelBubble=!0)))}}function E(e,t){const n=a["*"];let o=s(e,c,i);if(e.key&&"capslock"===e.key.toLowerCase())return;if(!(K.filter||g).call(this,e))return;if(93!==o&&224!==o||(o=91),-1===d.indexOf(o)&&229!==o&&d.push(o),["metaKey","ctrlKey","altKey","shiftKey"].forEach((t=>{const n=l[t];e[t]&&-1===d.indexOf(n)?d.push(n):!e[t]&&d.indexOf(n)>-1?d.splice(d.indexOf(n),1):"metaKey"===t&&e[t]&&(d=d.filter((e=>e in l||e===o)))})),o in f){f[o]=!0;for(const t in i)if(Object.prototype.hasOwnProperty.call(i,t)){const n=l[i[t]];K[t]=e[n]}if(!n)return}for(const t in f)Object.prototype.hasOwnProperty.call(f,t)&&(f[t]=e[l[t]]);e.getModifierState&&(!e.altKey||e.ctrlKey)&&e.getModifierState("AltGraph")&&(-1===d.indexOf(17)&&d.push(17),-1===d.indexOf(18)&&d.push(18),f[17]=!0,f[18]=!0);const r=w();if(n)for(let o=0;o<n.length;o++)n[o].scope===r&&("keydown"===e.type&&n[o].keydown||"keyup"===e.type&&n[o].keyup)&&v(e,n[o],r,t);if(!(o in a))return;const u=a[o],p=u.length;for(let n=0;n<p;n++)if(("keydown"===e.type&&u[n].keydown||"keyup"===e.type&&u[n].keyup)&&u[n].key){const o=u[n],{splitKey:s}=o,c=o.key.split(s),i=[];for(let e=0;e<c.length;e++)i.push(k(c[e]));i.sort().join("")===d.sort().join("")&&v(e,o,r,t)}}const K=function e(n,l,y){d=[];const m=r(n);let w=[],g="all",b=document,v=0,L=!1,C=!0,j="+",x=!1,_=!1;if(void 0===y&&"function"==typeof l&&(y=l),"[object Object]"===Object.prototype.toString.call(l)){const e=l;e.scope&&(g=e.scope),e.element&&(b=e.element),e.keyup&&(L=e.keyup),void 0!==e.keydown&&(C=e.keydown),void 0!==e.capture&&(x=e.capture),"string"==typeof e.splitKey&&(j=e.splitKey),!0===e.single&&(_=!0)}for("string"==typeof l&&(g=l),_&&O(n,g);v<m.length;v++){const e=m[v].split(j);w=[],e.length>1&&(w=o(i,e));let t=e[e.length-1];t="*"===t?"*":k(t),t in a||(a[t]=[]),a[t].push({keyup:L,keydown:C,scope:g,mods:w,shortcut:m[v],method:y,key:m[v],splitKey:j,element:b})}if(void 0!==b&&"undefined"!=typeof window){if(!h.has(b)){const e=(e=window.event)=>E(e,b),n=(e=window.event)=>{E(e,b),function(e){let t=s(e,c,i);e.key&&"capslock"===e.key.toLowerCase()&&(t=k(e.key));const n=d.indexOf(t);if(n>=0&&d.splice(n,1),e.key&&"meta"===e.key.toLowerCase()&&d.splice(0,d.length),93!==t&&224!==t||(t=91),t in f){f[t]=!1;for(const e in i)i[e]===t&&(K[e]=!1)}}(e)};h.set(b,{keydownListener:e,keyupListenr:n,capture:x}),t(b,"keydown",e,x),t(b,"keyup",n,x)}if(!u){const e=()=>{d=[]};u={listener:e,capture:x},t(window,"focus",e,x)}if(!p&&"undefined"!=typeof document){const t=()=>{d=[];for(const e in f)f[e]=!1;for(const t in i)e[t]=!1},n=t,o=t;document.addEventListener("fullscreenchange",n),document.addEventListener("webkitfullscreenchange",o),p={fullscreen:n,webkit:o}}}};function L(e){const t=Object.values(a).flat();if(t.findIndex((({element:t})=>t===e))<0&&e){const{keydownListener:t,keyupListenr:o,capture:r}=h.get(e)||{};t&&o&&(n(e,"keyup",o,r),n(e,"keydown",t,r),h.delete(e))}if(t.length<=0||h.size<=0){if(Array.from(h.keys()).forEach((e=>{const{keydownListener:t,keyupListenr:o,capture:r}=h.get(e)||{};t&&o&&(n(e,"keyup",o,r),n(e,"keydown",t,r),h.delete(e))})),h.clear(),Object.keys(a).forEach((e=>delete a[e])),u){const{listener:e,capture:t}=u;n(window,"focus",e,t),u=null}p&&"undefined"!=typeof document&&(document.removeEventListener("fullscreenchange",p.fullscreen),document.removeEventListener("webkitfullscreenchange",p.webkit),p=null)}}const C={getPressedKeyString:()=>d.map((e=>{return t=e,Object.keys(c).find((e=>c[e]===t))||(e=>Object.keys(i).find((t=>i[t]===e)))(e)||String.fromCharCode(e);var t})),setScope:m,getScope:w,deleteScope:(e,t)=>{let n,o;e||(e=w());for(const t in a)if(Object.prototype.hasOwnProperty.call(a,t))for(n=a[t],o=0;o<n.length;)if(n[o].scope===e){n.splice(o,1).forEach((({element:e})=>L(e)))}else o++;w()===e&&m(t||"all")},getPressedKeyCodes:()=>d.slice(0),getAllKeyCodes:()=>{const e=[];return Object.keys(a).forEach((t=>{a[t].forEach((({key:t,scope:n,mods:o,shortcut:r})=>{e.push({scope:n,shortcut:r,mods:o,keys:t.split("+").map((e=>k(e)))})}))})),e},isPressed:e=>("string"==typeof e&&(e=k(e)),-1!==d.indexOf(e)),filter:g,trigger:function(e,t="all"){Object.keys(a).forEach((n=>{a[n].filter((n=>n.scope===t&&n.shortcut===e)).forEach((e=>{e&&e.method&&e.method({},e)}))}))},unbind:O,keyMap:c,modifier:i,modifierMap:l};for(const e in C){const t=e;Object.prototype.hasOwnProperty.call(C,t)&&(K[t]=C[t])}if("undefined"!=typeof window){const e=window.hotkeys;K.noConflict=t=>(t&&window.hotkeys===K&&(window.hotkeys=e),K),window.hotkeys=K}return K}(); //# sourceMappingURL=hotkeys-js.min.js.map