forked from CSNLab/misc-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trigger_sender.py
69 lines (56 loc) · 2.06 KB
/
trigger_sender.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
#!/usr/bin/env python
#
# Meng Du
# October 2017
#
"""
This script automatically send triggers by generating keyboard presses.
Install pyautogui before running this script:
http://pyautogui.readthedocs.io/en/latest/install.html
It takes three command line arguments. Usage:
python trigger_sender.py <trigger key> <number of triggers> <time interval between two triggers (in seconds)>
Example:
python trigger_sender.py t 500 1
Note: Before starting actual key presses, the key will first be pressed 20 times
continuously to determine the average time required to press this key. Make sure
you discard the first 20 triggers. You can change the number below.
"""
from __future__ import print_function
import sys
import time
import timeit
import pyautogui
# the key will first be pressed for this number of times
# to calculate an average time required
NUM_PRE_RUN = 20
def main():
try:
# get command line arguments
key = sys.argv[1]
if key not in pyautogui.KEYBOARD_KEYS:
raise ValueError
num = int(sys.argv[2])
interval = float(sys.argv[3])
# get an average time of key press
def keypress():
pyautogui.press(key)
avg_time = timeit.timeit(keypress, number=NUM_PRE_RUN) / NUM_PRE_RUN
if avg_time > interval:
raise RuntimeError('Time required for the keyboard press is longer than '
'the requested time interval between triggers.')
interval -= avg_time
start_time = time.time()
# press key
for _ in range(num):
pyautogui.press(key)
time.sleep(interval)
duration = time.time() - start_time
print('\nPressed ' + str(num) + ' "' + key + '"s in ' + str(duration) + ' seconds\n')
except (IndexError, ValueError):
print('Invalid argument.\n'
'Usage: python trigger_sender.py '
'<trigger key> '
'<number of triggers> '
'<time interval between two triggers (in seconds)>')
if __name__ == '__main__':
main()