I posted this question a few days ago but I did not get an exact answer all I got is some unexplained code from the user, Jimmy Fraiture
Here is the code that he provided:
from pynput.keyboard import Listener
def on_press(key):
# check that it is the key you want and exit your script for example
with Listener(on_press=on_press) as listener:
listener.join()
# do your stuff here
while True:
pass
I got the code but I did not know how to use it like if I had a while loop how to I set up my space key to break this loop or to be the Keyboard Interrupt key please someone explain the code or provide an answer I would be very grateful
CodePudding user response:
is this what you are looking for?
import keyboard # using module keyboard
from pynput.keyboard import Listener
def on_press(key):
if keyboard.is_pressed("s"): # if key 's' is pressed
exit()
with Listener(on_press=on_press) as listener:
listener.join()
to use while
loop, you should remove the listener
import keyboard # using module keyboard
from pynput.keyboard import Listener
while True:
if keyboard.is_pressed("s"): # if key 's' is pressed
exit()