-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
57 lines (45 loc) · 1.67 KB
/
main.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
import time
from pynput import keyboard, mouse
start_time = None
terminate_program = False
def on_key_press(key):
global start_time, terminate_program
if key == keyboard.Key.esc:
# Signal to terminate the program
terminate_program = True
return False # Stop the keyboard listener
end_time = time.time()
lag = (end_time - start_time) * 1000 # Convert to milliseconds
print(f"Keyboard input lag for key {key}: {lag:.2f} ms")
def on_click(x, y, button, pressed):
global start_time
if pressed: # Measure on mouse press
end_time = time.time()
lag = (end_time - start_time) * 1000 # Convert to milliseconds
print(f"Mouse click lag for {button}: {lag:.2f} ms")
# Check if the program should terminate after a mouse event
if terminate_program:
return False # Stop the mouse listener
def main():
global start_time, terminate_program
print("Press any key or mouse button to measure input lag. Press ESC to exit.")
# Start the keyboard listener in a non-blocking fashion
keyboard_listener = keyboard.Listener(on_press=on_key_press)
keyboard_listener.start()
# Start the mouse listener in a non-blocking fashion
mouse_listener = mouse.Listener(on_click=on_click)
mouse_listener.start()
try:
while not terminate_program:
start_time = time.time()
time.sleep(0.1)
except KeyboardInterrupt:
print("Program interrupted.")
finally:
keyboard_listener.stop()
mouse_listener.stop()
keyboard_listener.join()
mouse_listener.join()
print("Exiting application...")
if __name__ == "__main__":
main()