Archive
monitoring key presses in a console application in a thread
Problem
I wrote a console application that shows a table and updates the screen every second. Later, I wanted to add a table sorting functionality. For instance, if you press ‘b’, then the table is sorted by the 2nd column, pressing ‘c’ would sort the table by the 3rd column, etc.
I found some keyboard monitoring libraries but they were all blocking, i.e. you had to call a function which was waiting for a key press. If you didn’t press any key, this function was just waiting.
However, in my program I had an infinite loop that was doing the following steps: (1) clear the screen, (2) draw the table, (3) repeat. If I add anywhere the keyboard monitoring, the loop gets blocked somewhere.
Solution
I asked this question on reddit (see here), and /u/Viddog4 suggested that I should use a thread. Of course! I have the main loop that draws the table, and I have a thread in the background that monitors the keyboard.
Here is a simplified code that demonstrates the idea:
#!/usr/bin/env python3
"""
pip3 install pynput xlib
"""
import threading
from time import sleep
from pynput.keyboard import Key, Listener
class myThread(threading.Thread):
def __init__(self, _id, name):
super().__init__()
self.daemon = True # daemon threads are killed as soon as the main program exits
self._id = _id
self.name = name
def on_press(self, key):
print('{0} pressed'.format(key))
def on_release(self, key):
print('{0} release'.format(key))
if key == Key.esc:
# Stop listener
return False
def run(self):
with Listener(on_press=self.on_press, on_release=self.on_release) as listener:
listener.join()
def main():
thread1 = myThread(1, "thread_1")
thread1.start()
# main loop:
while True:
print(".", flush=True)
try:
sleep(1)
except KeyboardInterrupt:
break
##########
if __name__ == "__main__":
main()
You can stop the thread with Esc. You can terminate the whole program with Ctrl+C. The thread is registered as a daemon thread, which means that if the main program exits (e.g. you press Ctrl+C), then daemon threads are automatically stopped.
Links
- pynput, keyboard monitoring library
- An Intro to Threading in Python, to learn more about threads
- Different ways to kill a Thread, I chose to set it as a daemon thread
