+ 1

need help to end loop using keyboard shortcut

### Hi guys, I need help to end the run_porgram() loop using a keyboard shortcut since my program is running in another window other than terminal using Ctrl+C is not working, I can set how many times it should run but sometimes I need to stop it earlier. ### import time import pyautogui, sys from time import sleep def search_cycle_increase(): pyautogui.click(1078, 799) # search sleep(0.8) pyautogui.click(1183, 743) # buy now sleep(0.12) pyautogui.click(724, 530) # ok sleep(0.12) pyautogui.click(123, 215) # back sleep(0.12) pyautogui.press('k') sleep(0.12) def search_cycle_decrease(): pyautogui.click(1078, 799) # search sleep(0.8) pyautogui.click(1183, 743) # buy now sleep(0.12) pyautogui.click(724, 530) # ok sleep(0.12) pyautogui.click(123, 215) # back sleep(0.12) pyautogui.press('l') sleep(0.12) def cycle_complete(): for i in range(0,5): search_cycle_increase() for i in range(0,5): search_cycle_decrease() def run_program(): run = input("How many times you want to run the program? ") x = 0 while x<=int(run): sleep(3) x+=1 cycle_complete() run_program()

24th Apr 2020, 1:49 PM
Sergiu Simion Faragau
Sergiu Simion Faragau - avatar
3 Respuestas
+ 1
The documentation is not clear about the 'fail-safes' feature, wich should be enabled by default (but the details differ according on where it's explained): https://pyautogui.readthedocs.io/en/latest/ https://pyautogui.readthedocs.io/en/latest/quickstart.html#fail-safes So, you could: 1) try moving the mouse cursor in any (primary, pyautogui seems to not work with multiple monitors) screen corner, or only in top-left corner: that should throw an exception ans stop the script running... 2) if not working (I cannot try, as I don't successed to install pyautogui), maybe the 'fail-safes' feature is not enabled by default, and you could try to set it explicitly pyautogui.FAILSAFE = True ... and retry to do the 1) 3) if you don't have enough time to move the mouse pointer (as it's moved by pyautogui), you could try to set up a 2.5 second pause after each PyAutoGUI call (as explained in the second link above): pyautogui.PAUSE = 2.5 ... and retry 1) and/or 2) Hope this would help ;)
24th Apr 2020, 8:40 PM
visph
visph - avatar
+ 1
thank you for your answers!
9th May 2020, 8:15 AM
Sergiu Simion Faragau
Sergiu Simion Faragau - avatar
0
pyautogui doesn't handle user keystroke... maybe you could look at tkinter module documentation (wich should be able to do that, and is a pyautogui dependency ^^)
24th Apr 2020, 8:43 PM
visph
visph - avatar