hotkeys-js
Version:
A simple micro-library for defining and dispatching keyboard shortcuts. It has no dependencies.
11 lines (10 loc) • 8.06 kB
JavaScript
var hotkeys=function(){"use strict";
/*!
* hotkeys-js v4.0.3
* 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){let t=e.keyCode||e.which||e.charCode;return e.code&&/^Key[A-Z]$/.test(e.code)&&(t=e.code.charCodeAt(3)),t}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=[],p=null,u=null,y="all";const h=new Map,m=e=>c[e.toLowerCase()]||i[e.toLowerCase()]||e.toUpperCase().charCodeAt(0),k=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?"*":m(l);if(!a[f])return;t||(t=w());const d=c>1?o(i,r):[],p=[];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&&p.push(e.element),!o})),p.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);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 c=a[o],p=c.length;for(let n=0;n<p;n++)if(("keydown"===e.type&&c[n].keydown||"keyup"===e.type&&c[n].keyup)&&c[n].key){const o=c[n],{splitKey:s}=o,i=o.key.split(s),l=[];for(let e=0;e<i.length;e++)l.push(m(i[e]));l.sort().join("")===d.sort().join("")&&v(e,o,r,t)}}const K=function e(n,c,l){d=[];const y=r(n);let k=[],w="all",g=document,b=0,v=!1,L=!0,C="+",j=!1,x=!1;if(void 0===l&&"function"==typeof c&&(l=c),"[object Object]"===Object.prototype.toString.call(c)){const e=c;e.scope&&(w=e.scope),e.element&&(g=e.element),e.keyup&&(v=e.keyup),void 0!==e.keydown&&(L=e.keydown),void 0!==e.capture&&(j=e.capture),"string"==typeof e.splitKey&&(C=e.splitKey),!0===e.single&&(x=!0)}for("string"==typeof c&&(w=c),x&&O(n,w);b<y.length;b++){const e=y[b].split(C);k=[],e.length>1&&(k=o(i,e));let t=e[e.length-1];t="*"===t?"*":m(t),t in a||(a[t]=[]),a[t].push({keyup:v,keydown:L,scope:w,mods:k,shortcut:y[b],method:l,key:y[b],splitKey:C,element:g})}if(void 0!==g&&"undefined"!=typeof window){if(!h.has(g)){const e=(e=window.event)=>E(e,g),n=(e=window.event)=>{E(e,g),function(e){let t=s(e);e.key&&"capslock"===e.key.toLowerCase()&&(t=m(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(g,{keydownListener:e,keyupListenr:n,capture:j}),t(g,"keydown",e,j),t(g,"keyup",n,j)}if(!p){const e=()=>{d=[]};p={listener:e,capture:j},t(window,"focus",e,j)}if(!u&&"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),u={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])),p){const{listener:e,capture:t}=p;n(window,"focus",e,t),p=null}u&&"undefined"!=typeof document&&(document.removeEventListener("fullscreenchange",u.fullscreen),document.removeEventListener("webkitfullscreenchange",u.webkit),u=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:k,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&&k(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=>m(e)))})}))})),e},isPressed:e=>("string"==typeof e&&(e=m(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