chefwink
chefwink

Reputation: 47

Python: How to kill a program using a keystroke (im playing around with pyautogui)

I know CTRL+C will kill my program, however I have a strict path where my mouse is going and clicking in a while loop, that I do not have time to click on the VS Code Windows , then click terminal, then click CTRL+C, so I was wondering the other best way to do this.

the idea is that I want my pyautogui code in the while loop to happen 300 times.

i=1
while (i<=300)

#do a bunch of stuff

#if the user presses 'esc' the program kills or breaks

Upvotes: 0

Views: 648

Answers (2)

Tanay
Tanay

Reputation: 661

You can try out this code :

import keyboard  # pip install keyboard
i = 1
while (i <= 300):
    if keyboard.is_pressed('esc'):
        break
print('loop finished')

Upvotes: 0

Filip M&#252;ller
Filip M&#252;ller

Reputation: 1180

Pyautogui has a feature that can probably be a good fit for your need. When you put your mouse cursor into one of the corners of the screen, it will trigger a fail-safe and the program will be killed. Read more in the docs.

Upvotes: 2

Related Questions