-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeybind.min.js
1 lines (1 loc) · 964 Bytes
/
keybind.min.js
1
class Keybind{#a(e){let t=new KeyboardEvent(e,{bubbles:!0});t.name=this.name,t.codes=this.codes,document.activeElement.dispatchEvent(t)}constructor(e){if("single"!==e.type&&"multi"!==e.type)throw TypeError("Invalid keybind type");this.type=e.type,this.name=e.name,this.set_manual(e.codes),this.p=new Set,this.s=!1;let t=e=>{!e.repeat&&("keydown"===e.type?this.p.add(e.code):this.p.delete(e.code),"keydown"===e.type&&!this.s&&[...this.codes].every(e=>this.p.has(e))&&this.#a("keybindpress"))};document.addEventListener("keydown",t),document.addEventListener("keyup",t)}set_detect(){this.s=!0,this.codes=new Set;let e=e=>{this.codes.add(e.code)};document.addEventListener("keydown",e),document.addEventListener("single"===this.type?"keydown":"keyup",()=>{document.removeEventListener("keydown",e),this.#a("keybindbound"),this.s=!1},{once:!0})}set_manual(e){if(0===e.size||e.size>1&&"single"===this.type)throw RangeError("Invalid keybind codes length");this.codes=e}}