| const Cast = require('../util/cast'); |
|
|
| |
| |
| |
| |
| const KEY_NAME = { |
| SPACE: 'space', |
| LEFT: 'left arrow', |
| UP: 'up arrow', |
| RIGHT: 'right arrow', |
| DOWN: 'down arrow', |
| ENTER: 'enter', |
| |
| BACKSPACE: 'backspace', |
| DELETE: 'delete', |
| SHIFT: 'shift', |
| CAPS_LOCK: 'caps lock', |
| SCROLL_LOCK: 'scroll lock', |
| CONTROL: 'control', |
| ESCAPE: 'escape', |
| INSERT: 'insert', |
| HOME: 'home', |
| END: 'end', |
| PAGE_UP: 'page up', |
| PAGE_DOWN: 'page down' |
| }; |
|
|
| |
| |
| |
| |
| const KEY_NAME_SET = new Set(Object.values(KEY_NAME)); |
|
|
| class Keyboard { |
| constructor (runtime) { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| this._keysPressed = []; |
| |
| this._keysHit = []; |
| this._keysHitOnStep = {}; |
| |
| this._keyTimestamps = {}; |
| |
| |
| |
| |
| |
| this.runtime = runtime; |
| |
| this.lastKeyPressed = ''; |
| this._numeralKeyCodesToStringKey = new Map(); |
| |
| |
| this.runtime.on("RUNTIME_STEP_END", () => { |
| const newHitKeys = []; |
| for (const key of this._keysHit) { |
| const stepKeyPressedOn = this._keysHitOnStep[key] || -1; |
| if (this.runtime.frameLoop._stepCounter <= stepKeyPressedOn) { |
| newHitKeys.push(key); |
| } |
| } |
|
|
| |
| this._keysHit = newHitKeys; |
| }); |
| } |
|
|
| |
| |
| |
| |
| |
| _keyStringToScratchKey (keyString) { |
| keyString = Cast.toString(keyString); |
| |
| switch (keyString) { |
| case ' ': return KEY_NAME.SPACE; |
| case 'ArrowLeft': |
| case 'Left': return KEY_NAME.LEFT; |
| case 'ArrowUp': |
| case 'Up': return KEY_NAME.UP; |
| case 'Right': |
| case 'ArrowRight': return KEY_NAME.RIGHT; |
| case 'Down': |
| case 'ArrowDown': return KEY_NAME.DOWN; |
| case 'Enter': return KEY_NAME.ENTER; |
| |
| case 'Backspace': return KEY_NAME.BACKSPACE; |
| case 'Delete': return KEY_NAME.DELETE; |
| case 'Shift': return KEY_NAME.SHIFT; |
| case 'CapsLock': return KEY_NAME.CAPS_LOCK; |
| case 'ScrollLock': return KEY_NAME.SCROLL_LOCK; |
| case 'Control': return KEY_NAME.CONTROL; |
| case 'Escape': return KEY_NAME.ESCAPE; |
| case 'Insert': return KEY_NAME.INSERT; |
| case 'Home': return KEY_NAME.HOME; |
| case 'End': return KEY_NAME.END; |
| case 'PageUp': return KEY_NAME.PAGE_UP; |
| case 'PageDown': return KEY_NAME.PAGE_DOWN; |
| } |
| |
| if (keyString.length > 1) { |
| return ''; |
| } |
| |
| return keyString; |
| } |
|
|
| |
| |
| |
| |
| |
| _keyArgToScratchKey (keyArg) { |
| |
| if (typeof keyArg === 'number') { |
| |
| |
| if (keyArg >= 48 && keyArg <= 90) { |
| return String.fromCharCode(keyArg); |
| } |
| switch (keyArg) { |
| case 32: return KEY_NAME.SPACE; |
| case 37: return KEY_NAME.LEFT; |
| case 38: return KEY_NAME.UP; |
| case 39: return KEY_NAME.RIGHT; |
| case 40: return KEY_NAME.DOWN; |
| } |
| } |
|
|
| keyArg = Cast.toString(keyArg); |
|
|
| |
| |
| |
| if (keyArg.length > 1 && KEY_NAME_SET.has(keyArg)) { |
| return keyArg; |
| } |
|
|
| |
| if (keyArg.length > 1) { |
| keyArg = keyArg[0]; |
| } |
|
|
| |
| if (keyArg === ' ') { |
| return KEY_NAME.SPACE; |
| } |
| |
| |
| if (keyArg === '\r') { |
| |
| return KEY_NAME.ENTER; |
| } |
| if (keyArg === '\u001b') { |
| return KEY_NAME.ESCAPE; |
| } |
|
|
| return keyArg.toUpperCase(); |
| } |
|
|
| |
| |
| |
| |
| postData (data) { |
| if (!data.key) return; |
| |
| const scratchKeyCased = this._keyStringToScratchKey(data.key); |
| const scratchKey = scratchKeyCased.length === 1 ? scratchKeyCased.toUpperCase() : scratchKeyCased; |
| if (scratchKey === '') return; |
| const index = this._keysPressed.indexOf(scratchKey); |
| if (data.isDown) { |
| |
| this.lastKeyPressed = scratchKeyCased; |
| this.runtime.emit('KEY_PRESSED', scratchKey); |
| |
| if (index < 0) { |
| |
| this.runtime.emit('KEY_HIT', scratchKey); |
| this._keysPressed.push(scratchKey); |
| this._keyTimestamps[scratchKey] = Date.now(); |
| |
| this._keysHit.push(scratchKey); |
| this._keysHitOnStep[scratchKey] = this.runtime.frameLoop._stepCounter; |
| } |
| } else if (index > -1) { |
| |
| this._keysPressed.splice(index, 1); |
| if (scratchKey in this._keyTimestamps) { |
| delete this._keyTimestamps[scratchKey]; |
| } |
| } |
| |
| if (data.hasOwnProperty('keyCode')) { |
| const keyCode = data.keyCode; |
| if (this._numeralKeyCodesToStringKey.has(keyCode)) { |
| const lastKeyOfSameCode = this._numeralKeyCodesToStringKey.get(keyCode); |
| if (lastKeyOfSameCode !== scratchKey) { |
| const indexToUnpress = this._keysPressed.indexOf(lastKeyOfSameCode); |
| if (indexToUnpress !== -1) { |
| this._keysPressed.splice(indexToUnpress, 1); |
| if (scratchKey in this._keyTimestamps) { |
| delete this._keyTimestamps[lastKeyOfSameCode]; |
| } |
| } |
| } |
| } |
| this._numeralKeyCodesToStringKey.set(keyCode, scratchKey); |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| getKeyIsDown (keyArg) { |
| if (keyArg === 'any') { |
| return this._keysPressed.length > 0; |
| } |
| const scratchKey = this._keyArgToScratchKey(keyArg); |
| return this._keysPressed.indexOf(scratchKey) > -1; |
| } |
| |
| |
| |
| |
| |
| |
| getKeyIsHit (keyArg) { |
| if (keyArg === 'any') { |
| return this._keysHit.length > 0; |
| } |
| const scratchKey = this._keyArgToScratchKey(keyArg); |
| return this._keysHit.indexOf(scratchKey) > -1; |
| } |
|
|
| |
| getLastKeyPressed () { |
| return this.lastKeyPressed; |
| } |
| |
| getAllKeysPressed () { |
| return this._keysPressed; |
| } |
| getKeyTimestamp (keyArg) { |
| if (keyArg === 'any') { |
| |
| let oldestTimestamp = Infinity; |
| let found = false; |
| for (const keyName in this._keyTimestamps) { |
| const timestamp = this._keyTimestamps[keyName]; |
| if (timestamp < oldestTimestamp) { |
| oldestTimestamp = timestamp; |
| found = true; |
| } |
| } |
| if (!found) return 0; |
| return oldestTimestamp; |
| } |
| |
| const scratchKey = this._keyArgToScratchKey(keyArg); |
| if (!(scratchKey in this._keyTimestamps)) { |
| return 0; |
| } |
| return this._keyTimestamps[scratchKey]; |
| } |
| getKeyTimestamps () { |
| return this._keyTimestamps; |
| } |
| } |
|
|
| module.exports = Keyboard; |
|
|