123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- package remdeshid
- import (
- "fmt"
- "time"
- )
- func (c *Controller) IsModifierKeys(keycode int) bool {
- // Modifier keycodes for JavaScript
- modifierKeys := []int{16, 17, 18, 91} // Shift, Ctrl, Alt, Meta (Windows/Command key)
- for _, key := range modifierKeys {
- if keycode == key {
- return true
- }
- }
- return false
- }
- // ConstructAndSendCmd constructs a HID command based on the provided HIDCommand and sends it.
- func (c *Controller) ConstructAndSendCmd(HIDCommand *HIDCommand) ([]byte, error) {
- switch HIDCommand.Event {
- case EventTypeKeyPress:
- if IsModifierKey(uint8(HIDCommand.Keycode)) {
- //modifier keys
- return c.SetModifierKey(uint8(HIDCommand.Keycode), HIDCommand.IsRightModKey)
- } else if HIDCommand.Keycode == 13 && HIDCommand.IsRightModKey {
- // Numpad enter
- return c.SendKeyboardPress(uint8(146))
- }
- return c.SendKeyboardPress(uint8(HIDCommand.Keycode))
- case EventTypeKeyRelease:
- if IsModifierKey(uint8(HIDCommand.Keycode)) {
- //modifier keys
- return c.UnsetModifierKey(uint8(HIDCommand.Keycode), HIDCommand.IsRightModKey)
- } else if HIDCommand.Keycode == 13 && HIDCommand.IsRightModKey {
- // Numpad enter
- return c.SendKeyboardRelease(uint8(146))
- }
- return c.SendKeyboardRelease(uint8(HIDCommand.Keycode))
- case EventTypeMouseMove:
- if time.Now().UnixMilli()-c.lastCursorEventTime < MinCusorEventInterval {
- // Ignore mouse move events that are too close together
- return []byte{}, nil
- }
- c.lastCursorEventTime = time.Now().UnixMilli()
- if HIDCommand.MouseAbsX != 0 || HIDCommand.MouseAbsY != 0 {
- xLSB := byte(HIDCommand.MouseAbsX & 0xFF) // Extract LSB of X
- xMSB := byte((HIDCommand.MouseAbsX >> 8) & 0xFF) // Extract MSB of X
- yLSB := byte(HIDCommand.MouseAbsY & 0xFF) // Extract LSB of Y
- yMSB := byte((HIDCommand.MouseAbsY >> 8) & 0xFF) // Extract MSB of Y
- return c.MouseMoveAbsolute(xLSB, xMSB, yLSB, yMSB)
- } else if HIDCommand.MouseRelX != 0 || HIDCommand.MouseRelY != 0 {
- //Todo
- }
- return []byte{}, nil
- case EventTypeMousePress:
- if HIDCommand.MouseButton < 1 || HIDCommand.MouseButton > 3 {
- return nil, fmt.Errorf("invalid mouse button: %d", HIDCommand.MouseButton)
- }
- button := uint8(HIDCommand.MouseButton)
- return c.MouseButtonPress(button)
- case EventTypeMouseRelease:
- if HIDCommand.MouseButton < 1 || HIDCommand.MouseButton > 3 {
- return nil, fmt.Errorf("invalid mouse button: %d", HIDCommand.MouseButton)
- }
- button := uint8(HIDCommand.MouseButton)
- return c.MouseButtonRelease(button)
- case EventTypeMouseScroll:
- if time.Now().UnixMilli()-c.lastCursorEventTime < MinCusorEventInterval {
- // Ignore mouse move events that are too close together
- return []byte{}, nil
- }
- c.lastCursorEventTime = time.Now().UnixMilli()
- return c.MouseScroll(HIDCommand.MouseScroll)
- default:
- return nil, fmt.Errorf("unsupported HID command event type: %d", HIDCommand.Event)
- }
- }
|