UNPKG

hotkeys-js

Version:

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

11 lines (10 loc) • 10.3 kB
var hotkeys=function(){"use strict"; /*! * hotkeys-js v4.1.4 * 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}const s={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},c={16:"shiftKey",18:"altKey",17:"ctrlKey",91:"metaKey",shiftKey:16,ctrlKey:17,altKey:18,metaKey:91},l={16:!1,18:!1,17:!1,91:!1},a={};for(let e=1;e<20;e++)s[`f${e}`]=111+e;let f=[];const d=new Map,p=new WeakMap;let u=null,y=null,h=null,k="all";const m=new Map,w=e=>s[e.toLowerCase()]||i[e.toLowerCase()]||e.toUpperCase().charCodeAt(0),g=e=>{k=e||"all"},v=()=>k||"all",b=e=>{const t=e.composedPath&&e.composedPath()[0]||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};function O(){d.clear()}function E(e){const t=p.get(e);if(void 0!==t)return t;const n=e.code,o=n?d.get(n):void 0,r=void 0!==o?o:function(e,t,n){let o=e.keyCode||e.which||e.charCode;if(o&&e.code&&/^Numpad/.test(e.code))return"NumpadEnter"===e.code&&"num_enter"in t?t.num_enter:o;if(e.key){const o=e.key.toLowerCase();if(o in t)return t[o];if(o in n)return n[o];if(/^[0-9]$/.test(e.key))return e.key.charCodeAt(0);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}(e,s,i);return p.set(e,r),r}function K(e){let t=E(e);e.key&&"capslock"===e.key.toLowerCase()&&(t=w(e.key));const n=f.indexOf(t);if(n>=0&&f.splice(n,1),e.key&&"meta"===e.key.toLowerCase()?(f.splice(0,f.length),O()):e.code&&d.delete(e.code),93!==t&&224!==t||(t=91),t in l){l[t]=!1;for(const e in i)i[e]===t&&(_[e]=!1)}}const C=(e,...t)=>{if(void 0===e)Object.keys(a).forEach(e=>{Array.isArray(a[e])&&a[e].forEach(e=>L(e)),delete a[e]}),A(null);else if(Array.isArray(e))e.forEach(e=>{e.key&&L(e)});else if("object"==typeof e)e.key&&L(e);else if("string"==typeof e){let[n,o]=t;"function"==typeof n&&(o=n,n=""),L({key:e,scope:n,method:o,splitKey:"+"})}},L=({key:e,scope:t,method:n,splitKey:o="+"})=>{r(e).forEach(e=>{const r=e.split(o),s=r[r.length-1],i="*"===s?"*":w(s);if(!a[i])return;t||(t=v());const c=r.map(w),l=[];a[i]=a[i].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.key.split(e.splitKey).map(w),c);return o&&l.push(e.element),!o}),l.forEach(e=>A(e))})};function j(e,t,n,o,r,s){if(t.element!==o)return;let i;const c=0===t.mods.length&&1===t.key.split(t.splitKey).length&&w(t.key)in l;if(t.scope===n||"all"===t.scope){i=t.mods.length>0;for(const e in s)Object.prototype.hasOwnProperty.call(s,e)&&(!s[e]&&t.mods.indexOf(+e)>-1||s[e]&&-1===t.mods.indexOf(+e))&&(i=!1);(0!==t.mods.length||!c&&(s[16]||s[18]||s[17]||s[91]))&&!i&&"*"!==t.shortcut||(t.keys=[],t.keys=t.keys.concat(r),!1===t.method(e,t)&&(e.preventDefault?e.preventDefault():e.returnValue=!1,e.stopPropagation&&e.stopPropagation(),e.cancelBubble&&(e.cancelBubble=!0)))}}function x(e,t){const n=a["*"];let o=E(e);if(e.key&&"capslock"===e.key.toLowerCase())return;if(!(_.filter||b).call(this,e))return;if(93!==o&&224!==o||(o=91),"keydown"===e.type&&e.code&&229!==o&&!d.has(e.code)&&d.set(e.code,o),-1===f.indexOf(o)&&229!==o&&f.push(o),["metaKey","ctrlKey","altKey","shiftKey"].forEach(t=>{const n=c[t];e[t]&&-1===f.indexOf(n)?f.push(n):!e[t]&&f.indexOf(n)>-1?f.splice(f.indexOf(n),1):"metaKey"===t&&e[t]&&(f=f.filter(e=>e in c||e===o))}),o in l){l[o]=!0;for(const t in i)if(Object.prototype.hasOwnProperty.call(i,t)){const n=c[i[t]];_[t]=e[n]}if(!n&&!(o in a))return}for(const t in l)Object.prototype.hasOwnProperty.call(l,t)&&(l[t]=e[c[t]]);e.getModifierState&&(!e.altKey||e.ctrlKey)&&e.getModifierState("AltGraph")&&(-1===f.indexOf(17)&&f.push(17),-1===f.indexOf(18)&&f.push(18),l[17]=!0,l[18]=!0);const r="keyup"===e.type?f.slice():f;let s=l;if("keyup"===e.type){s={};for(const e in l)Object.prototype.hasOwnProperty.call(l,e)&&(s[e]=l[e])}"keyup"===e.type&&o in l&&(-1===r.indexOf(o)&&r.push(o),s[o]=!0),"keyup"===e.type&&K(e);const p=v();if(n)for(let o=0;o<n.length;o++)("keydown"===e.type&&n[o].keydown||"keyup"===e.type&&n[o].keyup)&&j(e,n[o],p,t,r,s);if(!(o in a))return;const u=a[o],y=u.length;for(let n=0;n<y;n++)if(("keydown"===e.type&&u[n].keydown||"keyup"===e.type&&u[n].keyup)&&u[n].key){const o=u[n],{splitKey:i}=o,c=o.key.split(i),l=[];for(let e=0;e<c.length;e++)l.push(w(c[e]));l.sort().join("")===r.slice().sort().join("")&&j(e,o,p,t,r,s)}}const _=function e(n,s,c){f=[],O();const d=r(n);let p=[],k="all",g=document,v=0,b=!1,E=!0,L="+",j=!1,_=!1;if(void 0===c&&"function"==typeof s&&(c=s),"[object Object]"===Object.prototype.toString.call(s)){const e=s;e.scope&&(k=e.scope),e.element&&(g=e.element),e.keyup&&(b=e.keyup),void 0!==e.keydown&&(E=e.keydown),void 0!==e.capture&&(j=e.capture),"string"==typeof e.splitKey&&(L=e.splitKey),!0===e.single&&(_=!0)}for("string"==typeof s&&(k=s),_&&C({key:n,scope:k,splitKey:L});v<d.length;v++){const e=d[v].split(L);p=[],e.length>1&&(p=o(i,e));let t=e[e.length-1];t="*"===t?"*":w(t),t in a||(a[t]=[]),a[t].push({keyup:b,keydown:E,scope:k,mods:p,shortcut:d[v],method:c,key:d[v],splitKey:L,element:g})}if(void 0!==g&&"undefined"!=typeof window){if(!m.has(g)){const e=(e=window.event)=>x(e,g),n=(e=window.event)=>{x(e,g),K(e)};m.set(g,{keydownListener:e,keyupListenr:n,capture:j}),t(g,"keydown",e,j),t(g,"keyup",n,j)}if(!u){const n=t=>{if(t.target===window){f=[],O();for(const e in l)l[e]=!1;for(const t in i)e[t]=!1}};u={listener:n,capture:j},t(window,"focus",n,j),t(window,"blur",n,j)}if(!y&&"undefined"!=typeof document){const t=()=>{f=[],O();for(const e in l)l[e]=!1;for(const t in i)e[t]=!1},n=t,o=t;document.addEventListener("fullscreenchange",n),document.addEventListener("webkitfullscreenchange",o),y={fullscreen:n,webkit:o}}h||(h=()=>{f=[],O();for(const e in l)l[e]=!1;for(const t in i)e[t]=!1},t(window,"afterprint",h))}};function A(e){const t=Object.values(a).flat(),o=t.findIndex(({element:t})=>t===e);if(t.length<=0||m.size<=0){if(Array.from(m.keys()).forEach(e=>{const{keydownListener:t,keyupListenr:o,capture:r}=m.get(e)||{};t&&o&&(n(e,"keyup",o,r),n(e,"keydown",t,r),m.delete(e))}),m.clear(),Object.keys(a).forEach(e=>delete a[e]),u){const{listener:e,capture:t}=u;n(window,"focus",e,t),n(window,"blur",e,t),u=null}return y&&"undefined"!=typeof document&&(document.removeEventListener("fullscreenchange",y.fullscreen),document.removeEventListener("webkitfullscreenchange",y.webkit),y=null),void(h&&(n(window,"afterprint",h),h=null))}if(o<0&&e){const{keydownListener:t,keyupListenr:o,capture:r}=m.get(e)||{};t&&o&&(n(e,"keyup",o,r),n(e,"keydown",t,r),m.delete(e))}}const P={getPressedKeyString:()=>f.map(e=>{return t=e,Object.keys(s).find(e=>s[e]===t)||(e=>Object.keys(i).find(t=>i[t]===e))(e)||String.fromCharCode(e);var t}),setScope:g,getScope:v,deleteScope:(e,t)=>{let n,o;e||(e=v());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})=>A(e))}else o++;v()===e&&g(t||"all")},getPressedKeyCodes:()=>f.slice(0),getAllKeyCodes:()=>{const e=[];return Object.keys(a).forEach(t=>{a[t].forEach(({key:t,scope:n,mods:o,shortcut:r,splitKey:s})=>{e.push({scope:n,shortcut:r,mods:o,keys:t.split(s).map(e=>w(e))})})}),e},isPressed:e=>("string"==typeof e&&(e=w(e)),-1!==f.indexOf(e)),filter:b,trigger:function(e,t="all"){const n=f,o={};for(const e in l)o[e]=l[e];const r={};for(const e in i)r[e]=_[e];const s=Object.values(a).flat(),c=s.find(n=>n.scope===t&&n.shortcut===e)||s.find(e=>e.scope===t&&"*"===e.shortcut),d=e.split(c?c.splitKey:"+"),p=d[d.length-1],u=w(p),y=/^[a-z]$/i.test(p)?`Key${p.toUpperCase()}`:/^[0-9]$/.test(p)?`Digit${p}`:"";try{f=d.map(w);for(const e in l)l[e]=-1!==f.indexOf(Number(e));for(const e in i)_[e]=l[i[e]];Object.keys(a).forEach(n=>{a[n].filter(n=>n.scope===t&&(n.shortcut===e||"*"===n.shortcut)).forEach(e=>{if(e&&"function"==typeof e.method){let t;try{t=document.createEvent("KeyboardEvent")}catch(e){t=document.createEvent("Event")}t.initKeyboardEvent?t.initKeyboardEvent("keydown",!1,!0,window,p,0,l[17],l[18],l[16],l[91]):t.initEvent("keydown",!1,!0),Object.defineProperties(t,{key:{value:p},code:{value:y},keyCode:{value:u},which:{value:u},target:{value:e.element},ctrlKey:{value:l[17]},altKey:{value:l[18]},shiftKey:{value:l[16]},metaKey:{value:l[91]}}),e.keys=f.slice(0),e.method(t,e)}})})}finally{f=n;for(const e in l)l[e]=o[e];for(const e in i)_[e]=r[e]}},unbind:C,keyMap:s,modifier:i,modifierMap:c};for(const e in P){const t=e;Object.prototype.hasOwnProperty.call(P,t)&&(_[t]=P[t])}if("undefined"!=typeof window){const e=window.hotkeys;_.noConflict=t=>(t&&window.hotkeys===_&&(window.hotkeys=e),_),window.hotkeys=_}return _}(); //# sourceMappingURL=hotkeys-js.min.js.map