if keyboard.is_pressed('q'): # if key 'q' is pressed
print('You Pressed A Key!')
break # finishing the loop
#pip install keyboard
import keyboard
while True:
# Do your stuff
if keyboard.is_pressed("q"):
# Key was pressed
break