Эх сурвалжийг харах

Added working keyboard kvm

Toby Chui 1 долоо хоног өмнө
parent
commit
f0121012c9

+ 17 - 0
remdeskd/mod/remdeshid/hidcomm.go

@@ -34,6 +34,18 @@ func ConvHIDCommandToBytes(cmd HIDCommand) ([]byte, error) {
 				return nil, fmt.Errorf("invalid function key: %v", cmd.Data)
 			}
 			return data, nil
+		} else if isModifierKey(cmd.Data) {
+			//Modifier Key
+			if cmd.EventSubType == FRONTEND_SUBTYPE_KEYBOARD_KEY_DOWN {
+				data = append(data, SUBTYPE_KEYBOARD_MODIFIER_PRESS)
+			} else {
+				data = append(data, SUBTYPE_KEYBOARD_MODIFIER_RELEASE)
+			}
+			data = append(data, modifierKeyToByte(cmd.Data))
+			if data[0] == 0xFF {
+				return nil, fmt.Errorf("invalid modifier key: %v", cmd.Data)
+			}
+			return data, nil
 		} else if isOtherKeys(cmd.Data) {
 			//Other Keys
 			if cmd.EventSubType == FRONTEND_SUBTYPE_KEYBOARD_KEY_DOWN {
@@ -60,6 +72,10 @@ func ConvHIDCommandToBytes(cmd HIDCommand) ([]byte, error) {
 			//Special Key: NumLock
 			//TODO: Implement NumLock
 			return nil, fmt.Errorf("numLock not implemented")
+		} else if cmd.Data == "ContextMenu" {
+			//Special Key: ContextMenu
+			//TODO: Implement ContextMenu
+			return nil, fmt.Errorf("ContextMenu not implemented")
 		} else if cmd.Data == "Ctrl+Alt+Del" {
 			//Special Key: Ctrl+Alt+Del
 			data = append(data, SUBTYPE_KEYBOARD_SPECIAL_CTRLALTDEL)
@@ -70,6 +86,7 @@ func ConvHIDCommandToBytes(cmd HIDCommand) ([]byte, error) {
 		}
 
 	} else if cmd.EventType == FRONTEND_OPR_TYPE_MOUSE_WRITE {
+		/* Mouse Write Event */
 		data = []byte{OPR_TYPE_MOUSE_WRITE}
 	} else {
 		return nil, fmt.Errorf("invalid HID command type: %s", cmd.EventType)

+ 46 - 2
remdeskd/mod/remdeshid/hidconv.go

@@ -40,8 +40,8 @@ const (
 	SUBTYPE_KEYBOARD_ASCII_WRITE        = 0x01
 	SUBTYPE_KEYBOARD_ASCII_PRESS        = 0x02
 	SUBTYPE_KEYBOARD_ASCII_RELEASE      = 0x03
-	SUBTYPE_KEYBOARD_MODIFIER_SET       = 0x04
-	SUBTYPE_KEYBOARD_MODIFIER_CLEAR     = 0x05
+	SUBTYPE_KEYBOARD_MODIFIER_PRESS     = 0x04
+	SUBTYPE_KEYBOARD_MODIFIER_RELEASE   = 0x05
 	SUBTYPE_KEYBOARD_FUNCTKEY_PRESS     = 0x06
 	SUBTYPE_KEYBOARD_FUNCTKEY_RELEASE   = 0x07
 	SUBTYPE_KEYBOARD_OTHERKEY_PRESS     = 0x08
@@ -70,6 +70,16 @@ const (
 	PAYLOAD_KEYBOARD_NUMPAD_MINUS   = 0x0E
 	PAYLOAD_KEYBOARD_NUMPAD_ENTER   = 0x0F
 	PAYLOAD_KEYBOARD_NUMPAD_NUMLOCK = 0x10
+
+	// Modifier Keys IDs
+	PAYLOAD_KEY_LEFT_CTRL   = 0x00
+	PAYLOAD_KEY_LEFT_SHIFT  = 0x01
+	PAYLOAD_KEY_LEFT_ALT    = 0x02
+	PAYLOAD_KEY_LEFT_GUI    = 0x03
+	PAYLOAD_KEY_RIGHT_CTRL  = 0x04
+	PAYLOAD_KEY_RIGHT_SHIFT = 0x05
+	PAYLOAD_KEY_RIGHT_ALT   = 0x06
+	PAYLOAD_KEY_RIGHT_GUI   = 0x07
 )
 
 // Response Codes
@@ -81,6 +91,40 @@ const (
 	RESP_NOT_IMPLEMENTED   = 0x04
 )
 
+//isModifierKey checks if a key is a modifier key
+func isModifierKey(key string) bool {
+	switch key {
+	case "LEFT_Shift", "RIGHT_Shift", "LEFT_Control", "RIGHT_Control", "LEFT_Alt", "RIGHT_Alt", "Meta", "ContextMenu":
+		return true
+	default:
+		return false
+	}
+}
+
+//Convert modifier key string to byte
+func modifierKeyToByte(key string) byte {
+	switch key {
+	case "LEFT_Shift":
+		return PAYLOAD_KEY_LEFT_SHIFT
+	case "RIGHT_Shift":
+		return PAYLOAD_KEY_RIGHT_SHIFT
+	case "LEFT_Control":
+		return PAYLOAD_KEY_LEFT_CTRL
+	case "RIGHT_Control":
+		return PAYLOAD_KEY_RIGHT_CTRL
+	case "LEFT_Alt":
+		return PAYLOAD_KEY_LEFT_ALT
+	case "RIGHT_Alt":
+		return PAYLOAD_KEY_RIGHT_ALT
+	case "Meta":
+		return PAYLOAD_KEY_LEFT_GUI
+	case "ContextMenu":
+		return PAYLOAD_KEY_RIGHT_GUI
+	default:
+		return 0xFF
+	}
+}
+
 //Is a key a function key
 func isFuncKey(key string) bool {
 	switch key {

+ 58 - 50
usbkvm/usbkvm_fw/keyboard_emu.ino

@@ -99,53 +99,61 @@ bool isBitSet(uint8_t value, uint8_t n) {
 }
 
 //Handle modifying key set or unset
-void keyboard_modifying_key_set(uint8_t key) {
-  if (key & 0x01) {  // Bit 0 = KEY_LEFT_CTRL
-    Keyboard_press(KEY_LEFT_CTRL);
-  } else {
-    Keyboard_release(KEY_LEFT_CTRL);
-  }
-
-  if (key & 0x02) {  // Bit 1 = KEY_LEFT_SHIFT
-    Keyboard_press(KEY_LEFT_SHIFT);
-  } else {
-    Keyboard_release(KEY_LEFT_SHIFT);
-  }
-
-  if (key & 0x04) {  // Bit 2 = KEY_LEFT_ALT
-    Keyboard_press(KEY_LEFT_ALT);
-  } else {
-    Keyboard_release(KEY_LEFT_ALT);
-  }
-
-  if (key & 0x08) {  // Bit 3 = KEY_LEFT_GUI
-    Keyboard_press(KEY_LEFT_GUI);
-  } else {
-    Keyboard_release(KEY_LEFT_GUI);
-  }
-
-  if (key & 0x10) {  // Bit 4 = KEY_RIGHT_CTRL
-    Keyboard_press(KEY_RIGHT_CTRL);
-  } else {
-    Keyboard_release(KEY_RIGHT_CTRL);
-  }
-
-  if (key & 0x20) {  // Bit 5 = KEY_RIGHT_SHIFT
-    Keyboard_press(KEY_RIGHT_SHIFT);
-  } else {
-    Keyboard_release(KEY_RIGHT_SHIFT);
-  }
-
-  if (key & 0x40) {  // Bit 6 = KEY_RIGHT_ALT
-    Keyboard_press(KEY_RIGHT_ALT);
-  } else {
-    Keyboard_release(KEY_RIGHT_ALT);
-  }
-
-  if (key & 0x80) {  // Bit 7 = KEY_RIGHT_GUI
-    Keyboard_press(KEY_RIGHT_GUI);
+void keyboard_modifying_key_set(bool isPress, uint8_t key) {
+  if (isPress) {
+    switch (key) {
+      case PAYLOAD_KEY_LEFT_CTRL:
+        Keyboard_press(KEY_LEFT_CTRL);
+        return;
+      case PAYLOAD_KEY_LEFT_SHIFT:
+        Keyboard_press(KEY_LEFT_SHIFT);
+        return;
+      case PAYLOAD_KEY_LEFT_ALT:
+        Keyboard_press(KEY_LEFT_ALT);
+        return;
+      case PAYLOAD_KEY_LEFT_GUI:
+        Keyboard_press(KEY_LEFT_GUI);
+        return;
+      case PAYLOAD_KEY_RIGHT_CTRL:
+        Keyboard_press(KEY_RIGHT_CTRL);
+        return;
+      case PAYLOAD_KEY_RIGHT_SHIFT:
+        Keyboard_press(KEY_RIGHT_SHIFT);
+        return;
+      case PAYLOAD_KEY_RIGHT_ALT:
+        Keyboard_press(KEY_RIGHT_ALT);
+        return;
+      case PAYLOAD_KEY_RIGHT_GUI:
+        Keyboard_press(KEY_RIGHT_GUI);
+        return;
+    }
   } else {
-    Keyboard_release(KEY_RIGHT_GUI);
+    switch (key) {
+      case PAYLOAD_KEY_LEFT_CTRL:
+        Keyboard_release(KEY_LEFT_CTRL);
+        return;
+      case PAYLOAD_KEY_LEFT_SHIFT:
+        Keyboard_release(KEY_LEFT_SHIFT);
+        return;
+      case PAYLOAD_KEY_LEFT_ALT:
+        Keyboard_release(KEY_LEFT_ALT);
+        return;
+      case PAYLOAD_KEY_LEFT_GUI:
+        Keyboard_release(KEY_LEFT_GUI);
+        return;
+      case PAYLOAD_KEY_RIGHT_CTRL:
+        Keyboard_release(KEY_RIGHT_CTRL);
+        return;
+      case PAYLOAD_KEY_RIGHT_SHIFT:
+        Keyboard_release(KEY_RIGHT_SHIFT);
+        return;
+      case PAYLOAD_KEY_RIGHT_ALT:
+        Keyboard_release(KEY_RIGHT_ALT);
+        return;
+      case PAYLOAD_KEY_RIGHT_GUI:
+        Keyboard_release(KEY_RIGHT_GUI);
+        return;
+    }
   }
 }
 
@@ -282,11 +290,11 @@ uint8_t keyboard_emulation(uint8_t subtype, uint8_t value) {
         return resp_invalid_key_value;
       Keyboard_release(value);
       return resp_ok;
-    case SUBTYPE_KEYBOARD_MODIFIER_SET:
-      keyboard_modifying_key_set(value);
+    case SUBTYPE_KEYBOARD_MODIFIER_PRESS:
+      keyboard_modifying_key_set(true, value);
       return resp_ok;
-    case SUBTYPE_KEYBOARD_MODIFIER_CLEAR:
-      keyboard_modifying_key_set(0x00);
+    case SUBTYPE_KEYBOARD_MODIFIER_RELEASE:
+      keyboard_modifying_key_set(false, value);
       return resp_ok;
     case SUBTYPE_KEYBOARD_FUNCTKEY_PRESS:
       if (!is_funckey(value))

+ 12 - 2
usbkvm/usbkvm_fw/usbkvm_fw.h

@@ -29,8 +29,8 @@
 #define SUBTYPE_KEYBOARD_ASCII_WRITE 0x01         // Write ASCII characters (32-127)
 #define SUBTYPE_KEYBOARD_ASCII_PRESS 0x02         // Press a key (ASCII 32-127)
 #define SUBTYPE_KEYBOARD_ASCII_RELEASE 0x03       // Release a key (ASCII 32-127)
-#define SUBTYPE_KEYBOARD_MODIFIER_SET 0x04        // Modifier key write (bit flags)
-#define SUBTYPE_KEYBOARD_MODIFIER_CLEAR 0x05      // Modifier key press (bit flags)
+#define SUBTYPE_KEYBOARD_MODIFIER_PRESS 0x04      // Modifier key write (bit flags)
+#define SUBTYPE_KEYBOARD_MODIFIER_RELEASE 0x05    // Modifier key press (bit flags)
 #define SUBTYPE_KEYBOARD_FUNCTKEY_PRESS 0x06      //Function key press
 #define SUBTYPE_KEYBOARD_FUNCTKEY_RELEASE 0x07    //Function key release
 #define SUBTYPE_KEYBOARD_OTHERKEY_PRESS 0x08      //Other keys press
@@ -41,6 +41,16 @@
 #define SUBTYPE_KEYBOARD_SPECIAL_RESET 0xFE       //Reset all keypress state
 #define SUBTYPE_KEYBOARD_SPECIAL_RESERVED 0xFF    //Reserved
 
+/* Modifier Keys IDs */
+#define PAYLOAD_KEY_LEFT_CTRL 0x00
+#define PAYLOAD_KEY_LEFT_SHIFT 0x01
+#define PAYLOAD_KEY_LEFT_ALT 0x02
+#define PAYLOAD_KEY_LEFT_GUI 0x03
+#define PAYLOAD_KEY_RIGHT_CTRL 0x04
+#define PAYLOAD_KEY_RIGHT_SHIFT 0x05
+#define PAYLOAD_KEY_RIGHT_ALT 0x06
+#define PAYLOAD_KEY_RIGHT_GUI 0x07
+
 /* Numpad Buttons IDs */
 #define PAYLOAD_NUMPAD_0 0x00
 #define PAYLOAD_NUMPAD_1 0x01