-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevice.py
More file actions
66 lines (52 loc) · 1.77 KB
/
device.py
File metadata and controls
66 lines (52 loc) · 1.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
import json
import usb_hid
from adafruit_hid.mouse import Mouse
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
from adafruit_hid.keycode import Keycode
mouse = Mouse(usb_hid.devices)
keyboard = Keyboard(usb_hid.devices)
keyboard_layout_us = KeyboardLayoutUS(keyboard)
class Device:
def __init__(self):
pass
def parse_coors(self, data: str) -> tuple:
arr = data.split()
try:
x = int(arr[0]) if arr and len(arr) > 0 else None
y = int(arr[1]) if arr and len(arr) > 1 else None
except:
x = 0
y = 0
return(x, y)
def move(self, coors) -> None:
(x, y) = self.parse_coors(coors)
mouse.move(x, y)
def click(self, button) -> None:
if button == "left":
mouse.click(Mouse.LEFT_BUTTON)
elif button == "right":
mouse.click(Mouse.RIGHT_BUTTON)
def write(self, text: str) -> None:
keyboard_layout_us.write(text)
def hit_enter(self) -> None:
keyboard.press(Keycode.RETURN)
keyboard.release(Keycode.RETURN)
def handle_events(self, msg: str)-> str:
try:
parsed = json.loads(msg)
type = parsed.get("type")
value = parsed.get("value")
if type == "move":
self.move(coors=value)
elif type == "click":
self.click(button=value)
elif type == "write":
self.write(text=value)
elif type == "enter":
self.hit_enter()
else:
print("Unknown message type:", type, value)
return value
except json.JSONDecodeError:
return "Failed to decode JSON"