Search code examples
pythonpyautogui

KeyboardInterrupt not catching exception


I've read through various threads and the official documentation and yet I can't seem to stop this thread by hotkey. I need to right-click on the running code in vscodes output window and select stop code run.

Ctrl+C and Delete aren't stopping the code from running. Ideally, I'd like to know how to change the keypress to stop the code from running.

I use this in open CV and I was hoping to something similar with pyautogui.

if cv.waitKey(1) == ord('q'):
    cv.destroyAllWindows()
    break` 

The loop I'm struggling with:

try:
    while True:   
        for image in image_list:
            name = image
            location = py.locateCenterOnScreen(image, confidence=0.9)
            print(name + " Found at: " + str(location))

except KeyboardInterrupt:
    pass
    print("Interrupted")

I think the exception is "inside" the loop so it should be catching but nothing happens when I try and exit/break.


Solution

  • I was using Run Code not Run Python File in Terminal.

    Run Code is provided by the extension Code Runner, which will only show results in OUTPUT and not accept input.