1

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.

Iguananaut
  • 21,810
  • 5
  • 50
  • 63
  • Is it possible that your shell is losing focus, causing the Ctrl-C event to go to a different window? – 0x5453 Sep 02 '21 at 12:42
  • 1
    Double checked, definitely still in focus. It doesn't actually change! –  Sep 02 '21 at 12:47
  • 1
    I think this probably has less to do with pyautogui, actually, than with whatever GUI toolkit you're using, which might have its own means of handling SIGINT that are not compatible with catching `KeyboardInterrupt`. See for example: https://stackoverflow.com/a/66788780/982257 – Iguananaut Sep 02 '21 at 12:56
  • I can use the inbuild vscode interrupt but I was hoping to be able to define my own hotkeys to stop specific threats rather than all threads/scripts at once. –  Sep 02 '21 at 13:00

1 Answers1

0

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.