Deserialize the binary representation of Keystroke. Check scancode validity.

This commit is contained in:
László Monda
2016-03-25 21:02:18 +01:00
parent b9fe33d5d6
commit 32adbf2124
2 changed files with 18 additions and 2 deletions

View File

@@ -1,9 +1,24 @@
/// <reference path="Serializable.ts" />
class KeystrokeAction implements Serializable {
private static firstValidScancode = 1;
private static lastValidScancode = 231;
scancode: number;
modifierMask: number;
static isScancodeValid(scancode) {
return KeystrokeAction.firstValidScancode <= scancode &&
scancode <= KeystrokeAction.lastValidScancode;
}
private static checkScancode(scancode) {
if (!KeystrokeAction.isScancodeValid(scancode)) {
throw 'Scancode ${scancode} is invalid';
}
}
fromJsObject(jsObject: any) {
this.scancode = jsObject.scancode;
this.modifierMask = jsObject.modifierMask;
@@ -17,6 +32,9 @@ class KeystrokeAction implements Serializable {
}
fromBinary(buffer: UhkBuffer) {
this.scancode = buffer.readUInt8();
KeystrokeAction.checkScancode(this.scancode);
this.modifierMask = buffer.readUInt8();
}
toBinary(buffer: UhkBuffer) {

View File

@@ -9,8 +9,6 @@ let keyActions = uhkConfig.keymaps[0].layers[0].modules[0].keyActions;
let ARRAY_LAST_ELEMENT_ID = 0;
let KEY_ACTION_ID_KEYSTROKE_SCANCODE_FIRST = 1;
let KEY_ACTION_ID_KEYSTROKE_SCANCODE_LAST = 231;
let KEY_ACTION_ID_SWITCH_LAYER = 232;
let KEY_ACTION_ID_DUAL_ROLE_KEYSTROKE_MOD = 233;
let KEY_ACTION_ID_DUAL_ROLE_KEYSTROKE_FN = 234;