I want to be able to press a key on my keyboard anytime I want and the loop should detect it. Right now, I have to keep pressing 'alt' or 'esc' in order for my loop to detect the input, sometime it doesn't detect because I am not pressing fast enough. I just want to press 'alt' or 'esc' once.
I tried using if statements inside and outside of while loop. I've added try and except. Here is my code so far, it is working but I have to keep pressing those buttons.
while True:
if keyboard.is_pressed('ctrl'):
prg_status = True
print("program started...")
# goes on until it is false
while prg_status:
try:
try:
# "0 results matching"
results = browser.find_element_by_xpath("//div[@class='summary-text summary-text__expanded']").text
except:
pass
finally:
num_rslt1: int = int(results[0])
# if first index of the string is greater than 0, then a load is found
if num_rslt1 > 0:
# if num_rslt1 > num_rslt2:
# playsound('audio.mp3')
# num_rslt2 = num_rslt1
clear_console()
print(results[:2], "load(s) found")
# playsound('audio.mp3')
if keyboard.is_pressed('esc'):
print('program terminated')
prg_status = False
if keyboard.is_pressed('alt'):
print('program paused')
time.sleep(10)
# click on the refresh icon every 5 sec
browser.find_element_by_class_name("reload-icon").click()
time.sleep(3)
# if there is an error, skip so that the program keeps running instead of crashing
except:
pass
else:
pass
I expect to press 'alt' or 'esc' only once and my loop should detect the input.
I am keep pressing the button and sometimes it is detecting and sometimes it is not.