|
|
@ -86,9 +86,9 @@ export class NiceInputButton { |
|
|
|
|
|
|
|
export class NiceInputKey { |
|
|
|
|
|
|
|
constructor (actionName, keyName) { |
|
|
|
constructor (actionName, keyCode) { |
|
|
|
this.actionName = actionName; |
|
|
|
this.keyName = keyName; |
|
|
|
this.keyCode = keyCode; |
|
|
|
this.isPressed = false; |
|
|
|
} |
|
|
|
} |
|
|
@ -120,34 +120,44 @@ export class NiceInput { |
|
|
|
this.buttons[actionName] = button; |
|
|
|
} |
|
|
|
|
|
|
|
addKey (actionName, keyName) { |
|
|
|
const key = new NiceInputKey (actionName, keyName); |
|
|
|
addKey (actionName, keyCode) { |
|
|
|
const key = new NiceInputKey (actionName, keyCode); |
|
|
|
this.keys[actionName] = key; |
|
|
|
} |
|
|
|
|
|
|
|
async onKeyDown (event) { |
|
|
|
this.log.debug('onKeyDown', 'key pressed', { event }); |
|
|
|
|
|
|
|
const key = this.keys[event.key]; |
|
|
|
if (!key) { return; } |
|
|
|
const actionName = Object |
|
|
|
.keys(this.keys) |
|
|
|
.find((key) => this.keys[key].keyCode === event.code) |
|
|
|
; |
|
|
|
if (!actionName) { return; } |
|
|
|
|
|
|
|
const key = this.keys[actionName]; |
|
|
|
key.isPressed = true; |
|
|
|
|
|
|
|
const button = this.buttons[key.actionName]; |
|
|
|
const button = this.buttons[actionName]; |
|
|
|
if (button) { |
|
|
|
button.classList.add('active'); |
|
|
|
button.element.classList.add('active'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
async onKeyUp (event) { |
|
|
|
this.log.debug('onKeyUp', 'key released', { event }); |
|
|
|
|
|
|
|
const key = this.keys[event.key]; |
|
|
|
if (!key) { return; } |
|
|
|
const actionName = Object |
|
|
|
.keys(this.keys) |
|
|
|
.find((key) => this.keys[key].keyCode === event.code) |
|
|
|
; |
|
|
|
if (!actionName) { return; } |
|
|
|
|
|
|
|
const key = this.keys[actionName]; |
|
|
|
key.isPressed = false; |
|
|
|
|
|
|
|
const button = this.buttons[key.actionName]; |
|
|
|
const button = this.buttons[actionName]; |
|
|
|
if (button) { |
|
|
|
button.classList.remove('active'); |
|
|
|
button.element.classList.remove('active'); |
|
|
|
} |
|
|
|
} |
|
|
|
} |