0

I'm relatively new to coding, and I made this program to repeat mouse strokes when I am on a certain webpage so that the process can be automated.

import pyautogui, time

inp = raw_input("Number input?")
iterations = raw_input("Iterations?")

def move(x, y):
    pyautogui.moveTo(x, y)

for i in range(int(iterations)):
    move(540,515)
    pyautogui.click()
    move(690,760)
    pyautogui.click()
    pyautogui.typewrite(inp)
    move(1200,790)
    pyautogui.click()
    move(1200,340)
    pyautogui.click()

If I inputted too many iterations or I need to control my mouse again before the iterations are complete, is there a way to stop the loop/iterations? I tried to use CTRL+BREAK, but it didn't stop the loop. I also tried to add KeyboardInterrupt by using CTRL+C, but I couldn't figure out how to do it for my script.

K Wu
  • 9
  • 1
  • What do you mean "I also tried to add KeyboardInterrupt by using CTRL+C"? You mean you don't know how to handle it and keep going? https://stackoverflow.com/questions/1112343/how-do-i-capture-sigint-in-python – Two-Bit Alchemist Sep 05 '16 at 03:21
  • you don't need to add anything to your code for CTRL-C to work – Jules G.M. Sep 05 '16 at 03:33
  • sometimes, CTRL-C doesn't work, but CTRL-Z does, allowing you to then kill the process with something like `kill %1` in bash. Replace %1 with %whateverthenumberofyourjob, obviously – Jules G.M. Sep 05 '16 at 03:34
  • @QuestionUpvoter Please take a moment to hover over the upvote button and read the tooltip. "It is useful and clear." This question is in no sense clear. Every single person who has commented or attempted an answer has requested clarification and has gotten none. – Two-Bit Alchemist Sep 05 '16 at 13:22
  • I should have made the question more clear. The program is created in PyCharm but I use it on a webpage to repeatedly perform a specific function by going to a certain coordinate, clicking, going to another coordinate, clicking, etc. I just don't know how to exit the loop when my command window is not active. – K Wu Sep 05 '16 at 15:26

1 Answers1

0

For ctrl + C to work, the active window must be the one with the terminal running python.

I guess that it may be tricky when you have your program clicking everywhere...

Now to make an answer worth your while, I introduce you to web browsing with python : http://selenium-python.readthedocs.io/getting-started.html#simple-usage

This way your program can run in background and you can kill it whenever you want.

Loïc
  • 11,804
  • 1
  • 31
  • 49