Now make it loop. To cancel loop, press J
- User
Now make it loop. To cancel loop, press J
Assistant
Here's the script modified to loop continuously until 'J' is pressed:
import pyautogui import time import keyboard # Set the distance to move in each direction distance = 100 # pixels duration = 0.5 # seconds for each move # Give user time to move mouse to starting position print("Starting in 3 seconds. Move your mouse to the starting position.") print("Press 'J' to stop the loop.") time.sleep(3) try: while True: # Loop continuously # Check if 'J' is pressed if keyboard.is_pressed('j'): print("J pressed, stopping loop!") break # Left click to start pyautogui.click() # Move down-right pyautogui.moveRel(distance, distance, duration=duration) pyautogui.click() # Move down-left pyautogui.moveRel(-distance, distance, duration=duration) pyautogui.click() # Move up-left pyautogui.moveRel(-distance, -distance, duration=duration) pyautogui.click() # Move up-right pyautogui.moveRel(distance, -distance, duration=duration) pyautogui.click() except KeyboardInterrupt: print("\nProgram terminated by user") print("Done!")
Requirements:
You'll need both
pyautoguiandkeyboardmodules:pip install pyautogui pip install keyboard
✅ What's changed:
- Added infinite loop with
while True - Added keyboard detection for 'J' key
- Added try-except block to handle interruptions gracefully
- The script will now:
- Loop the square pattern continuously
- Stop when 'J' is pressed
- Can also be stopped by moving mouse to any screen corner (PyAutoGUI failsafe)
⚠️ Notes:
- The
keyboardmodule might require root/admin privileges on some systems - You can still use the PyAutoGUI failsafe by moving your mouse to any corner of the screen
- The script can be stopped in three ways:
- Pressing 'J'
- Moving mouse to screen corner
- Using Ctrl+C in terminal
Would you like any adjustments to the timing or distance parameters?
- Added infinite loop with
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.