pip install keyboard import keyboard while True: # do something if keyboard.is_pressed("q"): print("q pressed, ending loop") break