The easiest way is to just interrupt it with the usual Ctrl-C (SIGINT).
try:
while True:
do_something()
except KeyboardInterrupt:
pass
Since Ctrl-C causes KeyboardInterrupt to be raised, just catch it outside the loop and ignore it.
The easiest way is to just interrupt it with the usual Ctrl-C (SIGINT).
try:
while True:
do_something()
except KeyboardInterrupt:
pass
Since Ctrl-C causes KeyboardInterrupt to be raised, just catch it outside the loop and ignore it.
There is a solution that requires no non-standard modules and is 100% transportable:
import _thread
def input_thread(a_list):
raw_input() # use input() in Python3
a_list.append(True)
def do_stuff():
a_list = []
_thread.start_new_thread(input_thread, (a_list,))
while not a_list:
stuff()
Hey everyone!,
atm i need to repeat some code but i am not to sure how, i think i have to use while loops. And i need it to repeat an infinite amout of times untill i press a button for instance "q"
import time
import pyautogui
import pydirectinput
import time
<While loop?>
time.sleep(5)
pydirectinput.keyDown('d')
time.sleep(3)
pydirectinput.keyUp('d')
time.sleep(31)
pydirectinput.leftClick(982, 876)
<If statment where i need to press q to stop the sript?>
Thank you to anyone who can help me :)
Videos
How would you terminate this while loop, if let's say the key 'c' is pressed? So far my research has led me to a keyboard interrupt exception but I'm not sure if that is what I want.
x = True
while x:
print("While loop is on...")
if any key is pressed: <--- LET'S SAY THE KEY 'C' IS PRESSED, HOW CAN I TERMINATE THE LOOP?
x = False
print("While loop is off...")I'm trying to create an autoclicker using the pyautogui library. I want to create a fail safe for the user to break out of the auto clicker in case the cursor is not able to be moved and the user can't stop the program. I'm using the keyboard library to implement a hotkey to stop the auto clicker from running, but I'm not getting the intended behavior. The click() function is only executed once and the program quits. I'm on a Arch Linux, and gave the program root privileges as mentioned in the readme of keyboard. I've tried other variations under the Invoking code when an event happens of the readme, but none of them worked.
def repeat_until_stop():
while True:
pyautogui.click()
event = keyboard.read_event()
if event.event_type == keyboard.KEY_DOWN and event.name == 'q':
breakI am making a timer that beeps every x seconds but the timer restarts during a certain keypress. The first part of the code gets the timer to start. Then it goes into a while loop for the timer. I want to interrupt the loop without pressing keyboard interrupt but rather another key.
Any help?Here is the code below
import time, winsound, keyboard
x = 0
while x == 0:
if keyboard.is_pressed(','):
x = x+1
while True:
try:
while x==1:
for i in range(29):
time.sleep(1)
print(i)
if i == 28:
winsound.Beep(300,250)
except KeyboardInterrupt:
continueOne more approach is Async. user input. In your case a db or a simple file would suffice.
Have a look at this question
Problem solved.
Due to the low value of time.sleep() the execution of the loop was almost impossible to stop, apparently with an higher value for the sleep() the application is more responsive to the user input and everything works.
You need to check if the key is pressed in your infinite loop. If it is pressed, you need to exit
while True:
time.sleep(total_time)
mouse.click()
if keyboard.is_pressed("f10"):
break
But this waits for the sleep function , so you'll need to hold f10 for total_time seconds.
You should use a loop to check for the key, rather than sleeping
import datetime
...
clicking = True
while clicking:
mouse.click()
s = datetime.datetime.now()
while ((datetime.datetime.now()-s).total_seconds() < total_time):
# This runs while the difference in time since you started the loop is less than the time you want to wait
if keyboard.is_pressed("f10"):
clicking = False
break
Use one thread to handle user input and another thread to do the clicking:
from threading import Thread
from msvcrt import getwch
import mouse
done = False
def auto_click():
print("Press \"q\" to quit")
global done
while True:
if getwch() == "q":
done = True
break
Thread( target = auto_click, daemon = True ).start()
while not done: # you can add whatever logic you want including a time gate here
mouse.click()