forked from george-jensen/record-and-play-pynput
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecord.py
70 lines (55 loc) · 1.95 KB
/
record.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
69
70
from pynput import mouse
from pynput import keyboard
import time
import json
import sys
n = len(sys.argv)
if n < 2:
exit("Takes an argument - name of recording")
if n > 2:
exit("Only takes one argument - name of recording")
if n == 2:
name_of_recording = str(sys.argv[1])
print("Scroll to end the recording")
storage = []
count = 0
def on_press(key):
try:
json_object = {'action':'pressed_key', 'key':key.char, '_time': time.time()}
except AttributeError:
if key == keyboard.Key.esc:
return False
json_object = {'action':'pressed_key', 'key':str(key), '_time': time.time()}
storage.append(json_object)
def on_release(key):
try:
json_object = {'action':'released_key', 'key':key.char, '_time': time.time()}
except AttributeError:
json_object = {'action':'released_key', 'key':str(key), '_time': time.time()}
storage.append(json_object)
def on_move(x, y):
if len(storage) >= 1:
if storage[-1]['action'] == "pressed" or (storage[-1]['action'] == "moved" and time.time() - storage[-1]['_time'] > 0.02):
json_object = {'action':'moved', 'x':x, 'y':y, '_time':time.time()}
storage.append(json_object)
def on_click(x, y, button, pressed):
json_object = {'action':'pressed' if pressed else 'released', 'button':str(button), 'x':x, 'y':y, '_time':time.time()}
storage.append(json_object)
def on_scroll(x, y, dx, dy):
print("Cancelling")
with open('data/{}.txt'.format(name_of_recording), 'w') as outfile:
json.dump(storage, outfile)
return False
# Collect events from keyboard until esc
# Collect events from mouse until scroll
keyboard_listener = keyboard.Listener(
on_press=on_press,
on_release=on_release)
mouse_listener = mouse.Listener(
on_click=on_click,
on_scroll=on_scroll,
on_move=on_move)
keyboard_listener.start()
mouse_listener.start()
keyboard_listener.join()
mouse_listener.join()