Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.py
More file actions
Latest commit
130 lines (111 loc) · 3.77 KB
/
Copy pathcode.py
File metadata and controls
130 lines (111 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
importtime
importboard
importusb_hid
importbusio
importjson
fromadafruit_hid.keyboardimportKeyboard
fromadafruit_hid.keyboard_layout_usimportKeyboardLayoutUS
fromadafruit_hid.keycodeimportKeycode
fromadafruit_hid.mouseimportMouse
# UART connection to control computer (via USB-to-serial adapter)
uart=busio.UART(board.GP0, board.GP1, baudrate=9600)
buffer=bytearray()
defread_uart_line():
globalbuffer
whileTrue:
data=uart.read(32)
ifdata:
buffer.extend(data)
ifb'\n'inbuffer:
line, _, remainder=buffer.partition(b'\n')
buffer=bytearray(remainder)
returnline.decode("utf-8").strip()
time.sleep(0.01)
deftry_parse_json(line):
try:
returnjson.loads(line)
exceptExceptionase:
print("JSON parse error:", e)
returnNone
# Initialize HID devices
time.sleep(1)
keyboard=Keyboard(usb_hid.devices)
keyboard_layout=KeyboardLayoutUS(keyboard)
mouse=Mouse(usb_hid.devices)
time.sleep(1)
print("HIDAgent ready")
whileTrue:
line=read_uart_line()
print("Received:", line)
command_json=try_parse_json(line)
ifcommand_jsonisNone:
uart.write("Error\n".encode("utf-8"))
continue
type_param=command_json.get("type", "")
x_param=command_json.get("x", 0)
y_param=command_json.get("y", 0)
text_param=command_json.get("text", "")
defmove_in_chunks(dx, dy, chunk=10, delay=0.01):
# Move the mouse by (dx, dy) in small steps to avoid iOS
# pointer acceleration kicking in on large single moves.
rem_x, rem_y=dx, dy
whilerem_x!=0orrem_y!=0:
step_x=max(-chunk, min(chunk, rem_x))
step_y=max(-chunk, min(chunk, rem_y))
mouse.move(x=step_x, y=step_y)
rem_x-=step_x
rem_y-=step_y
time.sleep(delay)
iftype_param=="click":
mouse.move(x=-1000, y=-1000)
time.sleep(0.1)
move_in_chunks(x_param, y_param)
time.sleep(0.2)
mouse.click(Mouse.LEFT_BUTTON)
eliftype_param=="move":
mouse.move(x=-1000, y=-1000)
time.sleep(0.1)
move_in_chunks(x_param, y_param)
eliftype_param=="type":
keyboard_layout.write(text_param)
eliftype_param=="key":
# Handle key names like "enter", "tab", "space"
key_map= {
"enter": Keycode.ENTER,
"return": Keycode.ENTER,
"tab": Keycode.TAB,
"escape": Keycode.ESCAPE,
"space": Keycode.SPACEBAR,
"delete": Keycode.DELETE,
"backspace": Keycode.BACKSPACE,
"up": Keycode.UP_ARROW,
"down": Keycode.DOWN_ARROW,
"left": Keycode.LEFT_ARROW,
"right": Keycode.RIGHT_ARROW,
}
key_code=key_map.get(text_param.lower(), None)
ifkey_code:
keyboard.press(key_code)
keyboard.release_all()
eliftype_param=="run":
keyboard.press(Keycode.LEFT_GUI, Keycode.SPACEBAR)
keyboard.release_all()
time.sleep(1)
keyboard_layout.write(text_param)
keyboard.press(Keycode.ENTER)
keyboard.release_all()
eliftype_param=="downarrow":
keyboard.press(Keycode.DOWN_ARROW)
keyboard.release_all()
eliftype_param=="dragdown"ortype_param=="dragup":
step=25iftype_param=="dragdown"else-25
mouse.move(x=-1000, y=-1000)
time.sleep(0.1)
move_in_chunks(x_param, y_param)
time.sleep(0.1)
mouse.press(Mouse.LEFT_BUTTON)
for_inrange(5):
time.sleep(0.05)
mouse.move(y=step)
mouse.release(Mouse.LEFT_BUTTON)
uart.write("Success\n".encode("utf-8"))