from pynput import keyboard, mouse
import random
import time
import os
trials = int(input('Number of trials?'))
random.seed()
cls = lambda: os.system('cls')
def on_release(key):
return key != keyboard.Key.esc
def on_click(x, y, button, pressed):
return not pressed
tr1 = lambda x : True
tr2 = lambda x,y : True
tr4 = lambda x,y,z,w : True
lst_mouse_press = []
lst_key_rel = []
for i in range(trials):
rand_time = random.randrange(2000,6000)/1000.
cls()
print('Press RMB')
print('Trial: ' + str(i))
time.sleep(rand_time)
print('NOW!')
t1 = time.perf_counter()
with mouse.Listener(on_move=tr2, on_click=on_click, on_scroll=tr4) as listener:
listener.join()
t2 = time.perf_counter()
lst_mouse_press.append(t2-t1)
for i in range(trials):
rand_time = random.randrange(2000,6000)/1000.
cls()
print('Release ESC')
print('Trial: ' + str(i))
time.sleep(rand_time)
print('NOW!')
t1 = time.perf_counter()
with keyboard.Listener(on_press=tr1, on_release=on_release) as listener:
listener.join()
t2 = time.perf_counter()
lst_key_rel.append(t2-t1)
avg = lambda x : round(1000.*sum(x)/len(x))
mouse_press_time = avg(lst_mouse_press)
key_rel_time = avg(lst_key_rel)
print('Mouse press: ' + str(mouse_press_time) + 'ms')
print('Key release: ' + str(key_rel_time) + 'ms')