hotkeys-js
Version:
A simple micro-library for defining and dispatching keyboard shortcuts. It has no dependencies.
11 lines (10 loc) • 8.63 kB
JavaScript
var hotkeys=function(){"use strict";
/*!
* hotkeys-js v4.1.2
* 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(o&&e.code&&/^Numpad/.test(e.code))return 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}const i={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},c={"⇧":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++)i[`f${e}`]=111+e;let d=[],p=null,u=null,y=null,h="all";const k=new Map,m=e=>i[e.toLowerCase()]||c[e.toLowerCase()]||e.toUpperCase().charCodeAt(0),w=e=>{h=e||"all"},g=()=>h||"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};const O=(e,...t)=>{if(void 0===e)Object.keys(a).forEach(e=>{Array.isArray(a[e])&&a[e].forEach(e=>v(e)),delete a[e]}),L(null);else if(Array.isArray(e))e.forEach(e=>{e.key&&v(e)});else if("object"==typeof e)e.key&&v(e);else if("string"==typeof e){let[n,o]=t;"function"==typeof n&&(o=n,n=""),v({key:e,scope:n,method:o,splitKey:"+"})}},v=({key:e,scope:t,method:n,splitKey:o="+"})=>{r(e).forEach(e=>{const r=e.split(o),s=r[r.length-1],i="*"===s?"*":m(s);if(!a[i])return;t||(t=g());const c=r.map(m),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(m),c);return o&&l.push(e.element),!o}),l.forEach(e=>L(e))})};function E(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 K(e,t){const n=a["*"];let o=s(e,i,c);if(e.key&&"capslock"===e.key.toLowerCase())return;if(!(C.filter||b).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 c)if(Object.prototype.hasOwnProperty.call(c,t)){const n=l[c[t]];C[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=g();if(n)for(let o=0;o<n.length;o++)("keydown"===e.type&&n[o].keydown||"keyup"===e.type&&n[o].keyup)&&E(e,n[o],r,t);if(!(o in a))return;const p=a[o],u=p.length;for(let n=0;n<u;n++)if(("keydown"===e.type&&p[n].keydown||"keyup"===e.type&&p[n].keyup)&&p[n].key){const o=p[n],{splitKey:s}=o,i=o.key.split(s),c=[];for(let e=0;e<i.length;e++)c.push(m(i[e]));c.sort().join("")===d.sort().join("")&&E(e,o,r,t)}}const C=function e(n,l,h){d=[];const w=r(n);let g=[],b="all",v=document,E=0,L=!1,j=!0,x="+",_=!1,A=!1;if(void 0===h&&"function"==typeof l&&(h=l),"[object Object]"===Object.prototype.toString.call(l)){const e=l;e.scope&&(b=e.scope),e.element&&(v=e.element),e.keyup&&(L=e.keyup),void 0!==e.keydown&&(j=e.keydown),void 0!==e.capture&&(_=e.capture),"string"==typeof e.splitKey&&(x=e.splitKey),!0===e.single&&(A=!0)}for("string"==typeof l&&(b=l),A&&O({key:n,scope:b,splitKey:x});E<w.length;E++){const e=w[E].split(x);g=[],e.length>1&&(g=o(c,e));let t=e[e.length-1];t="*"===t?"*":m(t),t in a||(a[t]=[]),a[t].push({keyup:L,keydown:j,scope:b,mods:g,shortcut:w[E],method:h,key:w[E],splitKey:x,element:v})}if(void 0!==v&&"undefined"!=typeof window){if(!k.has(v)){const e=(e=window.event)=>K(e,v),n=(e=window.event)=>{K(e,v),function(e){let t=s(e,i,c);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 c)c[e]===t&&(C[e]=!1)}}(e)};k.set(v,{keydownListener:e,keyupListenr:n,capture:_}),t(v,"keydown",e,_),t(v,"keyup",n,_)}if(!p){const n=t=>{if(t.target===window){d=[];for(const e in f)f[e]=!1;for(const t in c)e[t]=!1}};p={listener:n,capture:_},t(window,"focus",n,_),t(window,"blur",n,_)}if(!u&&"undefined"!=typeof document){const t=()=>{d=[];for(const e in f)f[e]=!1;for(const t in c)e[t]=!1},n=t,o=t;document.addEventListener("fullscreenchange",n),document.addEventListener("webkitfullscreenchange",o),u={fullscreen:n,webkit:o}}y||(y=()=>{d=[];for(const e in f)f[e]=!1;for(const t in c)e[t]=!1},t(window,"afterprint",y))}};function L(e){const t=Object.values(a).flat(),o=t.findIndex(({element:t})=>t===e);if(t.length<=0||k.size<=0){if(Array.from(k.keys()).forEach(e=>{const{keydownListener:t,keyupListenr:o,capture:r}=k.get(e)||{};t&&o&&(n(e,"keyup",o,r),n(e,"keydown",t,r),k.delete(e))}),k.clear(),Object.keys(a).forEach(e=>delete a[e]),p){const{listener:e,capture:t}=p;n(window,"focus",e,t),n(window,"blur",e,t),p=null}return u&&"undefined"!=typeof document&&(document.removeEventListener("fullscreenchange",u.fullscreen),document.removeEventListener("webkitfullscreenchange",u.webkit),u=null),void(y&&(n(window,"afterprint",y),y=null))}if(o<0&&e){const{keydownListener:t,keyupListenr:o,capture:r}=k.get(e)||{};t&&o&&(n(e,"keyup",o,r),n(e,"keydown",t,r),k.delete(e))}}const j={getPressedKeyString:()=>d.map(e=>{return t=e,Object.keys(i).find(e=>i[e]===t)||(e=>Object.keys(c).find(t=>c[t]===e))(e)||String.fromCharCode(e);var t}),setScope:w,getScope:g,deleteScope:(e,t)=>{let n,o;e||(e=g());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++;g()===e&&w(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,splitKey:s})=>{e.push({scope:n,shortcut:r,mods:o,keys:t.split(s).map(e=>m(e))})})}),e},isPressed:e=>("string"==typeof e&&(e=m(e)),-1!==d.indexOf(e)),filter:b,trigger:function(e,t="all"){Object.keys(a).forEach(n=>{a[n].filter(n=>n.scope===t&&(n.shortcut===e||"*"===n.shortcut)).forEach(e=>{e&&e.method&&e.method({},e)})})},unbind:O,keyMap:i,modifier:c,modifierMap:l};for(const e in j){const t=e;Object.prototype.hasOwnProperty.call(j,t)&&(C[t]=j[t])}if("undefined"!=typeof window){const e=window.hotkeys;C.noConflict=t=>(t&&window.hotkeys===C&&(window.hotkeys=e),C),window.hotkeys=C}return C}();
//# sourceMappingURL=hotkeys-js.min.js.map