0

When I use Try/except with keyboard interrupt in a simple while loop the except works fine, but when using it with something still pretty simple, but more involved it will now no longer pickup the keyboard interrupt so I dont have a way to break the loop manually if I want to.
QUESTION: What is the best way to get keyboardinterrupt to work with my script below?

CODE:

try:
    count = 0
    xyz = 0 
    while pyautogui.locateOnScreen('add.png', region=(288, 147, 560, 1016), grayscale=True)!=None:
        while pyautogui.locateOnScreen('add.png', region=(288, 147, 560, 1016), grayscale=True)!=None:
            count=count+1
            print (count)
            x, y = pyautogui.locateCenterOnScreen('add.png', region=(288, 147, 560, 1016), grayscale=True)
            pyautogui.click(x, y)
        w = 0
        while w < 17:
            pyautogui.click(945, 1166)
            w=w+1
        xyz=xyz + 1
        time.sleep(1)
except KeyboardInterrupt:
    print ("KeyboardInterrupt detected!")`
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
Brandon
  • 465
  • 3
  • 10
  • 19
  • https://stackoverflow.com/a/44264586/4180176 - second part of the answer – Joshua Nixon Jun 20 '17 at 14:15
  • I don't know `pyautogui`, but it may redirect ctrl-c to a clipboard copy operation. You could try sending sigint from a different console. – tdelaney Jun 20 '17 at 14:25
  • Please include your OS and how you're running the script (e.g. from a system shell or from an IDE or IDLE). – Eryk Sun Jun 20 '17 at 14:39
  • Im running it on the Python3.6.0 Shell, on Windows 7. On a simple loop that just loops through a sleep timer 10 times or something like that, ctrl+c works great, but on the program above the only way ctrl+c works is if I click into the shell window first, which is almost impossible because pyautogui is moving the mouse around and making clicks. Slowing it down with a sleep timer would make the program miserably slow to sit through... – Brandon Jun 20 '17 at 15:12
  • If the input focus is shifting around automatically, then your script needs to define a global hotkey that, for example, is mapped to a function that calls `_thread.interrupt_main`. – Eryk Sun Jun 20 '17 at 15:43

0 Answers0