-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathkeyboard_and_mouse.py
executable file
·68 lines (47 loc) · 1.74 KB
/
keyboard_and_mouse.py
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
from datetime import datetime
from pynput import keyboard
HIDEvents = []
from config import timestep # this will be sufficient?
# problem is the windows zooming factor.
# is it really the problem?
def on_press(key):
if type(key) != str:
key = str(key)
HIDEvents.append(('key_press', key))
def on_release(key):
if type(key) != str:
key = str(key)
HIDEvents.append(('key_release', key))
keyboard_listener = keyboard.Listener(on_press=on_press, on_release=on_release)
keyboard_listener.start()
from pynput import mouse
def on_move(x: int, y: int):
HIDEvents.append(("mouse_move", [x, y]))
def on_click(x: int, y: int, button: mouse.Button, pressed: bool):
HIDEvents.append(("mouse_click", [x, y, str(button), pressed]))
def on_scroll(x: int, y: int, dx: int, dy: int):
HIDEvents.append(("mouse_scroll", [x, y, dx, dy]))
# # ...or, in a non-blocking fashion:
listener = mouse.Listener(on_move=on_move, on_click=on_click, on_scroll=on_scroll)
listener.start()
# you may start that non-blocking. start some looping-forever thread for writing states to file.
import time
# import pyautogui
# import datetime
loopCount = 500
import jsonlines
print("RECORDING START")
from config import filePath
import datetime
world_start = datetime.datetime.now()
with jsonlines.open(filePath, 'w') as w:
for _ in range(loopCount):
time.sleep(timestep)
# as for screenshot, use mss instead of screenshot.
# screenshot = pyautogui.screenshot()
# shall you mark the time here.
state = dict(HIDEvents=HIDEvents) # also the image!
print("STATE?", state)
w.write(state)
HIDEvents = []
mouseloc = []