0

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.

WeeeHaaa
  • 475
  • 1
  • 4
  • 10
  • Possible duplicate of [How to break this loop in Python by detecting key press](https://stackoverflow.com/questions/22183115/how-to-break-this-loop-in-python-by-detecting-key-press) – Green Cloak Guy Jul 30 '19 at 15:58
  • A loop is not able to detect and jump somewhere based on input while other lines are being executed, it's just not what it is designed to do. One way you might accomplish this is having a thread listening for input and adding those to a queue then when the loop is between fetching results you check the queue for input. I'm sure there are better solutions and libraries to solve this problem though. – CMMCD Jul 30 '19 at 19:22

0 Answers0