diff --git a/model/KeyAction.ts b/model/KeyAction.ts
new file mode 100644
index 00000000..7b726f85
--- /dev/null
+++ b/model/KeyAction.ts
@@ -0,0 +1,30 @@
+///
+
+class KeyAction {
+
+ static fromJsObject(jsObject: any): KeyAction {
+ switch (jsObject.actionType) {
+ case 'keyStroke':
+ let keystrokeAction = new KeystrokeAction();
+ keystrokeAction.fromJsObject(jsObject);
+ return keystrokeAction;
+ default:
+ throw 'Unknown KeyAction actionType "${jsObject.actionType}"';
+ }
+ }
+
+ static fromBinary(buffer: UhkBuffer) {
+ let keyActionFirstByte = buffer.readUInt8();
+ buffer.backtrack();
+
+ if (KeystrokeAction.firstValidScancode <= keyActionFirstByte &&
+ keyActionFirstByte <= KeystrokeAction.lastValidScancode)
+ {
+ let keystrokeAction = new KeystrokeAction();
+ keystrokeAction.fromBinary(buffer);
+ return keystrokeAction;
+ } else {
+ throw 'Unknown KeyAction first byte "${keyActionFirstByte}"';
+ }
+ }
+}
diff --git a/model/KeystrokeAction.ts b/model/KeystrokeAction.ts
index 87da0e9b..650a478f 100644
--- a/model/KeystrokeAction.ts
+++ b/model/KeystrokeAction.ts
@@ -1,9 +1,10 @@
+///
///
-class KeystrokeAction implements Serializable {
+class KeystrokeAction extends KeyAction implements Serializable {
- private static firstValidScancode = 1;
- private static lastValidScancode = 231;
+ static firstValidScancode = 1;
+ static lastValidScancode = 231;
_scancode: number;
modifierMask: number;
diff --git a/model/Serializable.ts b/model/Serializable.ts
index deb2102c..48259cd9 100644
--- a/model/Serializable.ts
+++ b/model/Serializable.ts
@@ -2,7 +2,7 @@
interface Serializable {
fromJsObject(jsObject: any);
- toJsObject(): any;
fromBinary(buffer: UhkBuffer);
+ toJsObject(): any;
toBinary(buffer: UhkBuffer);
}
diff --git a/model/UhkBuffer.ts b/model/UhkBuffer.ts
index 88968cc5..f1c812d3 100644
--- a/model/UhkBuffer.ts
+++ b/model/UhkBuffer.ts
@@ -7,16 +7,19 @@ class UhkBuffer {
buffer: Buffer;
offset: number;
+ bytesToBacktrack: number;
constructor() {
this.offset = 0;
+ this.bytesToBacktrack = 0;
this.buffer = new Buffer(UhkBuffer.eepromSize);
this.buffer.fill(0);
}
readInt8(): number {
let value = this.buffer.readInt8(this.offset);
- this.offset += 1;
+ this.bytesToBacktrack = 1;
+ this.offset += this.bytesToBacktrack;
return value;
}
@@ -27,7 +30,8 @@ class UhkBuffer {
readUInt8(): number {
let value = this.buffer.readUInt8(this.offset);
- this.offset += 1;
+ this.bytesToBacktrack = 1;
+ this.offset += this.bytesToBacktrack;
return value;
}
@@ -38,7 +42,8 @@ class UhkBuffer {
readInt16(): number {
let value = this.buffer.readInt16LE(this.offset);
- this.offset += 2;
+ this.bytesToBacktrack = 2;
+ this.offset += this.bytesToBacktrack;
return value;
}
@@ -49,7 +54,8 @@ class UhkBuffer {
readUInt16(): number {
let value = this.buffer.readUInt16LE(this.offset);
- this.offset += 2;
+ this.bytesToBacktrack = 2;
+ this.offset += this.bytesToBacktrack;
return value;
}
@@ -60,7 +66,8 @@ class UhkBuffer {
readInt32(): number {
let value = this.buffer.readInt32LE(this.offset);
- this.offset += 4;
+ this.bytesToBacktrack = 4;
+ this.offset += this.bytesToBacktrack;
return value;
}
@@ -71,7 +78,8 @@ class UhkBuffer {
readUInt32(): number {
let value = this.buffer.readUInt32LE(this.offset);
- this.offset += 4;
+ this.bytesToBacktrack = 4;
+ this.offset += this.bytesToBacktrack;
return value;
}
@@ -88,6 +96,7 @@ class UhkBuffer {
}
let str = this.buffer.toString(UhkBuffer.stringEncoding, this.offset, stringByteLength);
+ this.bytesToBacktrack = stringByteLength;
this.offset += stringByteLength;
return str;
}
@@ -110,4 +119,9 @@ class UhkBuffer {
this.buffer.write(str, this.offset, stringByteLength, UhkBuffer.stringEncoding);
this.offset += stringByteLength;
}
+
+ backtrack(): void {
+ this.offset -= this.bytesToBacktrack;
+ this.bytesToBacktrack = 0;
+ }
}