From 22862189801d73c9f47351b52aceaec15dfe6b26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20Monda?= Date: Thu, 3 May 2018 00:28:35 +0200 Subject: [PATCH] Make long media key macro actions work. (#621) * Make long media key macro actions work. * fix: macro key action mapping --- .../macro-action/key-macro-action.ts | 38 +++++++++++++------ .../tab/key/macro-key.component.ts | 2 +- .../tab/keypress/keypress-tab.component.ts | 2 +- 3 files changed, 29 insertions(+), 13 deletions(-) diff --git a/packages/uhk-common/src/config-serializer/config-items/macro-action/key-macro-action.ts b/packages/uhk-common/src/config-serializer/config-items/macro-action/key-macro-action.ts index b845997f..672c12d4 100644 --- a/packages/uhk-common/src/config-serializer/config-items/macro-action/key-macro-action.ts +++ b/packages/uhk-common/src/config-serializer/config-items/macro-action/key-macro-action.ts @@ -1,4 +1,4 @@ -import { assertEnum, assertUInt8 } from '../../assert'; +import { assertEnum, assertUInt8, assertUInt16 } from '../../assert'; import { UhkBuffer } from '../../uhk-buffer'; import { KeyModifiers } from '../key-modifiers'; import { MacroAction, MacroActionId, MacroKeySubAction, macroActionType } from './macro-action'; @@ -20,12 +20,24 @@ export class KeyMacroAction extends MacroAction { @assertEnum(KeystrokeType) type: KeystrokeType; - @assertUInt8 - scancode: number; - @assertUInt8 modifierMask: number; + @assertUInt16 + private _scancode: number; + + set scancode(scancode: number) { + this._scancode = scancode; + if (this.type !== KeystrokeType.shortMedia && this.type !== KeystrokeType.longMedia) { + return; + } + this.type = scancode < 256 ? KeystrokeType.shortMedia : KeystrokeType.longMedia; + } + + get scancode() { + return this._scancode; + } + constructor(other?: KeyMacroAction) { super(); if (!other) { @@ -33,7 +45,7 @@ export class KeyMacroAction extends MacroAction { } this.action = other.action; this.type = other.type; - this.scancode = other.scancode; + this._scancode = other._scancode; this.modifierMask = other.modifierMask; } @@ -45,7 +57,7 @@ export class KeyMacroAction extends MacroAction { } else { this.type = KeystrokeType[jsObject.type]; } - this.scancode = jsObject.scancode; + this._scancode = jsObject.scancode; this.modifierMask = jsObject.modifierMask; return this; } @@ -58,7 +70,7 @@ export class KeyMacroAction extends MacroAction { this.type = keyMacroType & 0b11; keyMacroType >>= 2; if (keyMacroType & 0b10) { - this.scancode = buffer.readUInt8(); + this._scancode = this.type === KeystrokeType.longMedia ? buffer.readUInt16() : buffer.readUInt8(); } if (keyMacroType & 0b01) { this.modifierMask = buffer.readUInt8(); @@ -78,7 +90,7 @@ export class KeyMacroAction extends MacroAction { } else { jsObject.type = KeystrokeType[this.type]; } - jsObject.scancode = this.scancode; + jsObject.scancode = this._scancode; } if (this.hasModifiers()) { @@ -98,7 +110,11 @@ export class KeyMacroAction extends MacroAction { buffer.writeUInt8(keyMacroType); if (this.hasScancode()) { - buffer.writeUInt8(this.scancode); + if (this.type === KeystrokeType.longMedia) { + buffer.writeUInt16(this.scancode); + } else { + buffer.writeUInt8(this.scancode); + } } if (this.hasModifiers()) { buffer.writeUInt8(this.modifierMask); @@ -106,7 +122,7 @@ export class KeyMacroAction extends MacroAction { } toString(): string { - return ``; + return ``; } isModifierActive(modifier: KeyModifiers): boolean { @@ -114,7 +130,7 @@ export class KeyMacroAction extends MacroAction { } hasScancode(): boolean { - return !!this.scancode; + return !!this._scancode; } hasModifiers(): boolean { diff --git a/packages/uhk-web/src/app/components/macro/action-editor/tab/key/macro-key.component.ts b/packages/uhk-web/src/app/components/macro/action-editor/tab/key/macro-key.component.ts index 110ef2f6..67285a8b 100644 --- a/packages/uhk-web/src/app/components/macro/action-editor/tab/key/macro-key.component.ts +++ b/packages/uhk-web/src/app/components/macro/action-editor/tab/key/macro-key.component.ts @@ -67,7 +67,7 @@ export class MacroKeyTabComponent extends MacroBaseComponent implements OnInit { } getKeyMacroAction(): KeyMacroAction { - const keyMacroAction = Object.assign(new KeyMacroAction(), this.keypressTab.toKeyAction()); + const keyMacroAction = new KeyMacroAction(this.keypressTab.toKeyAction() as any); keyMacroAction.action = this.getActionType(this.activeTab); return keyMacroAction; } diff --git a/packages/uhk-web/src/app/components/popover/tab/keypress/keypress-tab.component.ts b/packages/uhk-web/src/app/components/popover/tab/keypress/keypress-tab.component.ts index 2b1eb8ea..be4d2ab3 100644 --- a/packages/uhk-web/src/app/components/popover/tab/keypress/keypress-tab.component.ts +++ b/packages/uhk-web/src/app/components/popover/tab/keypress/keypress-tab.component.ts @@ -115,7 +115,7 @@ export class KeypressTabComponent extends Tab implements OnChanges { const scTypePair = this.toScancodeTypePair(this.selectedScancodeOption); keystrokeAction.scancode = scTypePair[0]; if (scTypePair[1] === 'media') { - keystrokeAction.type = KeystrokeType.shortMedia; + keystrokeAction.type = keystrokeAction.scancode > 255 ? KeystrokeType.longMedia : KeystrokeType.shortMedia; } else { keystrokeAction.type = KeystrokeType[scTypePair[1]]; }