I would like this Python 3.10 script (where the pynput
code is partially based on this answer) to enter the while
loop and at the same time monitor the keys pressed on the keyboard. When q
is pressed, I would like it to end.
(I do not know threads very well, but the while loop
probably should run in the main thread and the keybord monitor should run in a child, concurrent thread).
#!/usr/bin/python3
import threading
import sys
from pynput import keyboard
def on_key_press(key):
try:
k = key.char
except:
k = key.name
if k in ['q']:
exit_time = True
exit_time = False
print("Press q to close.")
keyboard_listener = keyboard.Listener(on_press=on_key_press)
keyboard_listener.start()
keyboard_listener.join()
while not exit_time:
sleep(1)
print("Goodbye")
sys.exit(0)
It instead gets locked in an endless wait after keyboard_listener.start()
. I don't know if keyboard_listener.join()
doesn't run at all, or if it causes the program to lock.
However, the while
loop is not run. If I end the program with Ctrl+C:
^CTraceback (most recent call last):
File "/my/source/./file.py", line 22, in <module>
keyboard_listener.join()
File "/my/.local/lib/python3.10/site-packages/pynput/_util/__init__.py", line 295, in join
super(AbstractListener, self).join(timeout, *args)
File "/usr/lib/python3.10/threading.py", line 1096, in join
self._wait_for_tstate_lock()
File "/usr/lib/python3.10/threading.py", line 1116, in _wait_for_tstate_lock
if lock.acquire(block, timeout):
KeyboardInterrupt
join
means ? did you read the docs ?while
loop really empty ? or are you doing any work inside it ? so is all the work done insideon_key_press
, or are you doing other work at the same time ?exit_time = True
is only local, you need to bring the global into scope withglobal exit_time
.... not that this will solve your actual bug.global
. In this example,while
is empty, but I would also like to try doing some work inside it, asynchronously, and not onon_key_press
.