simple keypress action macro working w/ macro type determination
This commit is contained in:
55
config-serializer/config-items/MacroAction.ts
Normal file
55
config-serializer/config-items/MacroAction.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
enum MacroActionId {
|
||||
PressKeyAction = 0,
|
||||
HoldKeyAction = 1,
|
||||
ReleaseKeyAction = 2,
|
||||
PressModifiersAction = 3,
|
||||
HoldModifiersAction = 4,
|
||||
ReleaseModifiersAction = 5,
|
||||
PressMouseButtonsAction = 6,
|
||||
HoldMouseButtonsAction = 7,
|
||||
ReleaseMouseButtonsAction = 8,
|
||||
MoveMouseAction = 9,
|
||||
ScrollMouseAction = 10,
|
||||
DelayAction = 11,
|
||||
TextAction = 12
|
||||
}
|
||||
|
||||
let macroActionType = {
|
||||
PressKeyAction : 'pressKey',
|
||||
HoldKeyAction : 'holdKey',
|
||||
ReleaseKeyAction : 'releaseKey',
|
||||
PressModifiersAction : 'pressModifiers',
|
||||
HoldModifiersAction : 'holdModifiers',
|
||||
ReleaseModifiersAction : 'releaseModifiers',
|
||||
PressMouseButtonsAction : 'pressMouseButtons',
|
||||
HoldMouseButtonsAction : 'holdMouseButtons',
|
||||
ReleaseMouseButtonsAction : 'releaseMouseButtons',
|
||||
MoveMouseAction : 'moveMouse',
|
||||
ScrollMouseAction : 'scrollMouse',
|
||||
DelayAction : 'delay',
|
||||
TextAction : 'text'
|
||||
};
|
||||
|
||||
abstract class MacroAction extends Serializable<MacroAction> {
|
||||
assertMacroActionType(jsObject: any) {
|
||||
let macroActionClassname = this.constructor.name;
|
||||
let macroActionTypeString = macroActionType[macroActionClassname];
|
||||
if (jsObject.macroActionType !== macroActionTypeString) {
|
||||
throw `Invalid ${macroActionClassname}.macroActionType: ${jsObject.macroActionType}`;
|
||||
}
|
||||
}
|
||||
|
||||
readAndAssertMacroActionId(buffer: UhkBuffer) {
|
||||
let classname = this.constructor.name;
|
||||
let readMacroActionId = buffer.readUInt8();
|
||||
let macroActionId = MacroActionId[<string> classname];
|
||||
if (readMacroActionId !== macroActionId) {
|
||||
throw `Invalid ${classname} first byte: ${readMacroActionId}`;
|
||||
}
|
||||
}
|
||||
|
||||
abstract _fromJsObject(jsObject: any): MacroAction;
|
||||
abstract _fromBinary(buffer: UhkBuffer): MacroAction;
|
||||
abstract _toJsObject(): any;
|
||||
abstract _toBinary(buffer: UhkBuffer): void;
|
||||
}
|
||||
28
config-serializer/config-items/MacroActions.ts
Normal file
28
config-serializer/config-items/MacroActions.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
class MacroActions extends ClassArray<MacroAction> {
|
||||
|
||||
jsObjectToClass(jsObject: any): Serializable<MacroAction> {
|
||||
switch (jsObject.macroActionType) {
|
||||
case macroActionType.PressKeyAction:
|
||||
return new PressKeyAction().fromJsObject(jsObject);
|
||||
default:
|
||||
throw `Invalid MacroAction.macroActionType: "${jsObject.macroActionType}"`;
|
||||
}
|
||||
}
|
||||
|
||||
binaryToClass(buffer: UhkBuffer): Serializable<MacroAction> {
|
||||
let macroActionFirstByte = buffer.readUInt8();
|
||||
buffer.backtrack();
|
||||
|
||||
if (buffer.enableDump) {
|
||||
process.stdout.write(']\n');
|
||||
buffer.enableDump = false;
|
||||
}
|
||||
|
||||
switch (macroActionFirstByte) {
|
||||
case MacroActionId.PressKeyAction:
|
||||
return new PressKeyAction().fromBinary(buffer);
|
||||
default:
|
||||
throw `Invalid MacroAction first byte: ${macroActionFirstByte}`;
|
||||
}
|
||||
}
|
||||
}
|
||||
33
config-serializer/config-items/PressKeyAction.ts
Normal file
33
config-serializer/config-items/PressKeyAction.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
class PressKeyAction extends MacroAction {
|
||||
|
||||
// @assertUInt8
|
||||
scancode: number;
|
||||
|
||||
_fromJsObject(jsObject: any): PressKeyAction {
|
||||
this.assertMacroActionType(jsObject);
|
||||
this.scancode = jsObject.scancode;
|
||||
return this;
|
||||
}
|
||||
|
||||
_fromBinary(buffer: UhkBuffer): PressKeyAction {
|
||||
this.readAndAssertMacroActionId(buffer);
|
||||
this.scancode = buffer.readUInt8();
|
||||
return this;
|
||||
}
|
||||
|
||||
_toJsObject(): any {
|
||||
return {
|
||||
macroActionType: macroActionType.PressKeyAction,
|
||||
scancode: this.scancode
|
||||
};
|
||||
}
|
||||
|
||||
_toBinary(buffer: UhkBuffer) {
|
||||
buffer.writeUInt8(MacroActionId.PressKeyAction);
|
||||
buffer.writeUInt8(this.scancode);
|
||||
}
|
||||
|
||||
toString(): string {
|
||||
return `<PressKeyAction scancode="${this.scancode}">`;
|
||||
}
|
||||
}
|
||||
@@ -15,3 +15,6 @@
|
||||
/// <reference path="Layers.ts" />
|
||||
/// <reference path="KeyMap.ts" />
|
||||
/// <reference path="KeyMaps.ts" />
|
||||
/// <reference path="MacroAction.ts" />
|
||||
/// <reference path="MacroActions.ts" />
|
||||
/// <reference path="PressKeyAction.ts" />
|
||||
|
||||
@@ -10,8 +10,8 @@ let fs = require('fs');
|
||||
|
||||
let uhkConfig = JSON.parse(fs.readFileSync('uhk-config.json'));
|
||||
|
||||
let config1Js = uhkConfig.keymaps;
|
||||
let config1Ts: Serializable<KeyMaps> = new KeyMaps().fromJsObject(config1Js);
|
||||
let config1Js = uhkConfig.macros[0].macroActions;
|
||||
let config1Ts: Serializable<MacroActions> = new MacroActions().fromJsObject(config1Js);
|
||||
let config1Buffer = new UhkBuffer();
|
||||
config1Ts.toBinary(config1Buffer);
|
||||
let config1BufferContent = config1Buffer.getBufferContent();
|
||||
@@ -19,7 +19,7 @@ fs.writeFileSync('uhk-config.bin', config1BufferContent);
|
||||
|
||||
config1Buffer.offset = 0;
|
||||
console.log();
|
||||
let config2Ts = new KeyMaps().fromBinary(config1Buffer);
|
||||
let config2Ts = new MacroActions().fromBinary(config1Buffer);
|
||||
console.log('\n');
|
||||
let config2Js = config2Ts.toJsObject();
|
||||
let config2Buffer = new UhkBuffer();
|
||||
|
||||
@@ -202,56 +202,6 @@
|
||||
{
|
||||
"macroActionType": "pressKey",
|
||||
"scancode": 111
|
||||
},
|
||||
{
|
||||
"macroActionType": "holdKey",
|
||||
"scancode": 111
|
||||
},
|
||||
{
|
||||
"macroActionType": "releaseKey",
|
||||
"scancode": 111
|
||||
},
|
||||
{
|
||||
"macroActionType": "pressModifiers",
|
||||
"modifierMask": 111
|
||||
},
|
||||
{
|
||||
"macroActionType": "holdModifiers",
|
||||
"modifierMask": 111
|
||||
},
|
||||
{
|
||||
"macroActionType": "releaseModifiers",
|
||||
"modifierMask": 111
|
||||
},
|
||||
{
|
||||
"macroActionType": "pressMouseButtons",
|
||||
"mouseButtonsMask": 9
|
||||
},
|
||||
{
|
||||
"macroActionType": "holdMouseButtons",
|
||||
"mouseButtonsMask": 9
|
||||
},
|
||||
{
|
||||
"macroActionType": "releaseMouseButtons",
|
||||
"mouseButtonsMask": 9
|
||||
},
|
||||
{
|
||||
"macroActionType": "moveMouse",
|
||||
"x": 123,
|
||||
"y": 123
|
||||
},
|
||||
{
|
||||
"macroActionType": "scrollMouse",
|
||||
"x": 123,
|
||||
"y": 123
|
||||
},
|
||||
{
|
||||
"macroActionType": "delay",
|
||||
"delay": "1000"
|
||||
},
|
||||
{
|
||||
"macroActionType": "text",
|
||||
"text": "this is a text"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user