I have tried some solutions that I could get from the other threads here but still no luck. My issue is that the program can recognize the character I've entered but does not execute to stop the program from running in loop. Appreciate any help in advance. Thanks!
from pynput import keyboard
import sys
from time import sleep
def stop_program():
sys.exit()
def run_program():
while True:
print('Program is running now... press \'x\' to stop')
sleep(1)
def on_press(key):
try:
if str(key) == "'x'":
stop_program()
except AttributeError:
pass
#non-blocking
listener = keyboard.Listener(
on_press=on_press)
listener.start()
run_program()