@animech-public/playcanvas
Version:
PlayCanvas WebGL game engine
2 lines (1 loc) • 3.04 kB
JavaScript
import{EventHandler as e}from"../../core/event-handler.js";import{KeyboardEvent as t}from"./keyboard-event.js";const i=new t;function n(e){return i.key=e.keyCode,i.element=e.target,i.event=e,i}function s(e){return"string"==typeof e?e.toUpperCase().charCodeAt(0):e}const r={9:"Tab",13:"Enter",16:"Shift",17:"Control",18:"Alt",27:"Escape",37:"Left",38:"Up",39:"Right",40:"Down",46:"Delete",91:"Win"};class a extends e{constructor(e,t={}){super(),this._element=null,this._keymap={},this._lastmap={},this._keyDownHandler=this._handleKeyDown.bind(this),this._keyUpHandler=this._handleKeyUp.bind(this),this._keyPressHandler=this._handleKeyPress.bind(this),this._visibilityChangeHandler=this._handleVisibilityChange.bind(this),this._windowBlurHandler=this._handleWindowBlur.bind(this),e&&this.attach(e),this.preventDefault=t.preventDefault||!1,this.stopPropagation=t.stopPropagation||!1}attach(e){this._element&&this.detach(),this._element=e,this._element.addEventListener("keydown",this._keyDownHandler,!1),this._element.addEventListener("keypress",this._keyPressHandler,!1),this._element.addEventListener("keyup",this._keyUpHandler,!1),document.addEventListener("visibilitychange",this._visibilityChangeHandler,!1),window.addEventListener("blur",this._windowBlurHandler,!1)}detach(){this._element&&(this._element.removeEventListener("keydown",this._keyDownHandler),this._element.removeEventListener("keypress",this._keyPressHandler),this._element.removeEventListener("keyup",this._keyUpHandler),this._element=null,document.removeEventListener("visibilitychange",this._visibilityChangeHandler,!1),window.removeEventListener("blur",this._windowBlurHandler,!1))}toKeyIdentifier(e){e=s(e);const t=r[e.toString()];if(t)return t;let i=e.toString(16).toUpperCase();const n=i.length;for(let e=0;e<4-n;e++)i=`0${i}`;return`U+${i}`}_handleKeyDown(e){const t=e.keyCode||e.charCode;if(void 0===t)return;const i=this.toKeyIdentifier(t);this._keymap[i]=!0,this.fire("keydown",n(e)),this.preventDefault&&e.preventDefault(),this.stopPropagation&&e.stopPropagation()}_handleKeyUp(e){const t=e.keyCode||e.charCode;if(void 0===t)return;const i=this.toKeyIdentifier(t);delete this._keymap[i],this.fire("keyup",n(e)),this.preventDefault&&e.preventDefault(),this.stopPropagation&&e.stopPropagation()}_handleKeyPress(e){this.fire("keypress",n(e)),this.preventDefault&&e.preventDefault(),this.stopPropagation&&e.stopPropagation()}_handleVisibilityChange(){"hidden"===document.visibilityState&&this._handleWindowBlur()}_handleWindowBlur(){this._keymap={},this._lastmap={}}update(){for(const e in this._lastmap)delete this._lastmap[e];for(const e in this._keymap)this._keymap.hasOwnProperty(e)&&(this._lastmap[e]=this._keymap[e])}isPressed(e){const t=s(e),i=this.toKeyIdentifier(t);return!!this._keymap[i]}wasPressed(e){const t=s(e),i=this.toKeyIdentifier(t);return!!this._keymap[i]&&!this._lastmap[i]}wasReleased(e){const t=s(e),i=this.toKeyIdentifier(t);return!this._keymap[i]&&!!this._lastmap[i]}}a.EVENT_KEYDOWN="keydown",a.EVENT_KEYUP="keyup";export{a as Keyboard};