ch9329.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package remdeshid
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. func (c *Controller) IsModifierKeys(keycode int) bool {
  7. // Modifier keycodes for JavaScript
  8. modifierKeys := []int{16, 17, 18, 91} // Shift, Ctrl, Alt, Meta (Windows/Command key)
  9. for _, key := range modifierKeys {
  10. if keycode == key {
  11. return true
  12. }
  13. }
  14. return false
  15. }
  16. // ConstructAndSendCmd constructs a HID command based on the provided HIDCommand and sends it.
  17. func (c *Controller) ConstructAndSendCmd(HIDCommand *HIDCommand) ([]byte, error) {
  18. switch HIDCommand.Event {
  19. case EventTypeKeyPress:
  20. if IsModifierKey(uint8(HIDCommand.Keycode)) {
  21. //modifier keys
  22. return c.SetModifierKey(uint8(HIDCommand.Keycode), HIDCommand.IsRightModKey)
  23. } else if HIDCommand.Keycode == 13 && HIDCommand.IsRightModKey {
  24. // Numpad enter
  25. return c.SendKeyboardPress(uint8(146))
  26. }
  27. return c.SendKeyboardPress(uint8(HIDCommand.Keycode))
  28. case EventTypeKeyRelease:
  29. if IsModifierKey(uint8(HIDCommand.Keycode)) {
  30. //modifier keys
  31. return c.UnsetModifierKey(uint8(HIDCommand.Keycode), HIDCommand.IsRightModKey)
  32. } else if HIDCommand.Keycode == 13 && HIDCommand.IsRightModKey {
  33. // Numpad enter
  34. return c.SendKeyboardRelease(uint8(146))
  35. }
  36. return c.SendKeyboardRelease(uint8(HIDCommand.Keycode))
  37. case EventTypeMouseMove:
  38. if time.Now().UnixMilli()-c.lastCursorEventTime < MinCusorEventInterval {
  39. // Ignore mouse move events that are too close together
  40. return []byte{}, nil
  41. }
  42. c.lastCursorEventTime = time.Now().UnixMilli()
  43. if HIDCommand.MouseAbsX != 0 || HIDCommand.MouseAbsY != 0 {
  44. xLSB := byte(HIDCommand.MouseAbsX & 0xFF) // Extract LSB of X
  45. xMSB := byte((HIDCommand.MouseAbsX >> 8) & 0xFF) // Extract MSB of X
  46. yLSB := byte(HIDCommand.MouseAbsY & 0xFF) // Extract LSB of Y
  47. yMSB := byte((HIDCommand.MouseAbsY >> 8) & 0xFF) // Extract MSB of Y
  48. return c.MouseMoveAbsolute(xLSB, xMSB, yLSB, yMSB)
  49. } else if HIDCommand.MouseRelX != 0 || HIDCommand.MouseRelY != 0 {
  50. //Todo
  51. }
  52. return []byte{}, nil
  53. case EventTypeMousePress:
  54. if HIDCommand.MouseButton < 1 || HIDCommand.MouseButton > 3 {
  55. return nil, fmt.Errorf("invalid mouse button: %d", HIDCommand.MouseButton)
  56. }
  57. button := uint8(HIDCommand.MouseButton)
  58. return c.MouseButtonPress(button)
  59. case EventTypeMouseRelease:
  60. if HIDCommand.MouseButton < 1 || HIDCommand.MouseButton > 3 {
  61. return nil, fmt.Errorf("invalid mouse button: %d", HIDCommand.MouseButton)
  62. }
  63. button := uint8(HIDCommand.MouseButton)
  64. return c.MouseButtonRelease(button)
  65. case EventTypeMouseScroll:
  66. if time.Now().UnixMilli()-c.lastCursorEventTime < MinCusorEventInterval {
  67. // Ignore mouse move events that are too close together
  68. return []byte{}, nil
  69. }
  70. c.lastCursorEventTime = time.Now().UnixMilli()
  71. return c.MouseScroll(HIDCommand.MouseScroll)
  72. default:
  73. return nil, fmt.Errorf("unsupported HID command event type: %d", HIDCommand.Event)
  74. }
  75. }