tinykeys
Version:
A tiny (~650 B) & modern library for keybindings.
3 lines (2 loc) • 1.32 kB
JavaScript
let e=["Shift","Meta","Alt","Control"],t="object"==typeof navigator?navigator.platform:"",n=/Mac|iPod|iPhone|iPad/.test(t),o=n?"Meta":"Control",i="Win32"===t?["Control","Alt"]:n?["Alt"]:[];function r(e,t){return"function"==typeof e.getModifierState&&(e.getModifierState(t)||i.includes(t)&&e.getModifierState("AltGraph"))}function a(e){return e.trim().split(" ").map(e=>{let t=e.split(/\b\+/),n=t.pop(),i=n.match(/^\((.+)\)$/);return i&&(n=new RegExp(`^${i[1]}$`)),t=t.map(e=>"$mod"===e?o:e),[t,n]})}function l(t,[n,o]){return!((o instanceof RegExp?!o.test(t.key)&&!o.test(t.code):o.toUpperCase()!==t.key.toUpperCase()&&o!==t.code)||n.find(e=>!r(t,e))||e.find(e=>!n.includes(e)&&o!==e&&r(t,e)))}function u(e,t={}){var n;let o=null!=(n=t.timeout)?n:1e3,i=Object.keys(e).map(t=>[a(t),e[t]]),u=new Map,c=null;return e=>{e instanceof KeyboardEvent&&(i.forEach(t=>{let n=t[0],o=t[1],i=u.get(n)||n;l(e,i[0])?i.length>1?u.set(n,i.slice(1)):(u.delete(n),o(e)):r(e,e.key)||u.delete(n)}),c&&clearTimeout(c),c=setTimeout(u.clear.bind(u),o))}}function c(e,t,{event:n="keydown",capture:o,timeout:i}={}){let r=u(t,{timeout:i});return e.addEventListener(n,r,o),()=>{e.removeEventListener(n,r,o)}}export{u as createKeybindingsHandler,l as matchKeyBindingPress,a as parseKeybinding,c as tinykeys};
//# sourceMappingURL=tinykeys.modern.js.map