-
Notifications
You must be signed in to change notification settings - Fork 434
/
segmented_macro.py
54 lines (45 loc) · 1.59 KB
/
segmented_macro.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
"""
This script allows you to record keyboard events in segments, then replay them
back with pauses between the segments.
It's useful for presentations, to ensure typing accuracy while still giving you
time to speak between segments.
"""
import sys
sys.path.append('../')
import keyboard
import pickle
import os
if len(sys.argv) == 1:
filename = input('Enter filename to save/load events: ')
else:
filename = sys.argv[1]
if os.path.exists(filename):
segments = pickle.load(open(filename, 'rb'))
for i, segment in enumerate(segments):
print('Press F1 to play segment {}/{}'.format(i+1, len(segments)))
print('Duration: {:.02} seconds'.format(segment[-1].time - segment[0].time))
keyboard.wait('F1')
keyboard.play(segment)
else:
print('Press F1 to save this fragment. Press F2 to discard it. Press F3 to stop recording.')
segments = []
segment = []
def handle_event(event):
global segment
if keyboard.matches(event, 'F1'):
if event.event_type == keyboard.KEY_DOWN:
if segment:
segments.append(segment)
segment = []
print('Saved', len(segments))
elif keyboard.matches(event, 'F2'):
if event.event_type == keyboard.KEY_DOWN:
segment = []
print('Discarded')
else:
segment.append(event)
keyboard.hook(handle_event)
keyboard.wait('F3')
keyboard.hook(handle_event)
pickle.dump(segments, open(filename, 'wb'))
print('Saved {} segments to {}'.format(len(segments), filename))