1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| from pynput import keyboard
import time
tinit = time.time()
def callb(key): #what to do on key-release
ti1 = str(time.time() - t)[0:8] #converting float to str, slicing the float
ti2= str(time.time() - tinit)[0:8] #converting float to str, slicing the float
print("At",ti2,"The key",key," is pressed for",ti1,'seconds')
return False #stop detecting more key-releases
def callb1(key): #what to do on key-press
return False #stop detecting more key-presses
with keyboard.Listener(on_press = callb1) as listener1: #setting code for listening key-press
listener1.join()
t = time.time() #reading time in sec
with keyboard.Listener(on_release = callb) as listener: #setting code for listening key-release
listener.join() |
Partager