0

I am making an auto clicker everything is working, but I have one problem. When I start the auto clicker, I have no way to exit it because my cursor is focused on a different window. This isn't ideal because when debugging it I might input a large value and then have to wait for it to finish before I can do anything else. Is there someway I could make it so when I hit a key the script stops even if I'm not focused on the window?

The full code can be seen here but the minimal example is:

def click(x, y, z):
    for i in range(z):
        win32api.SetCursorPos((x,y))
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)
        time.sleep(.03)

click(0,0,10000) #for example
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
  • I think you need the [solution from here](http://stackoverflow.com/questions/5592500/detecting-key-presses-using-win32api-in-python) – Tadhg McDonald-Jensen Mar 09 '16 at 20:29
  • 1
    Please paste your code into the body of the question itself. – PM 2Ring Mar 09 '16 at 20:29
  • 1
    the top of provided link says "# This is copied directly from StackOverflow" could you provide a link to the original source? – Tadhg McDonald-Jensen Mar 09 '16 at 20:30
  • @TadhgMcDonald-Jensen Lol forgot about that. [Here](http://stackoverflow.com/questions/1181464/controlling-mouse-with-python) – Gregory Dolan Mar 09 '16 at 20:52
  • @TadhgMcDonald-Jensen only part I copied was the click, everything else is mine – Gregory Dolan Mar 09 '16 at 20:53
  • and the `click` is modified from the original so you didn't actually directly copy it did you? still think the solution I linked to in my first comment is what you need. just check if the user has pressed some key combination in between the repeated clicks. – Tadhg McDonald-Jensen Mar 09 '16 at 20:58
  • @TadhgMcDonald-Jensen Yea I made this at like 1 last night. Didn't get a chance to look at your link yet. Thanks in advance – Gregory Dolan Mar 09 '16 at 21:00
  • You are using the wrong tool altogether. If you want to automate a UI you should be using [UI Automation](https://msdn.microsoft.com/en-us/library/windows/desktop/ee684009.aspx). That way you don't have to shuffle input focus around, and the problem you are trying to solve won't even exist. – IInspectable Mar 10 '16 at 10:16

0 Answers0