Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to control the mouse in Mac using Python?

What would be the easiest way to move the mouse around (and possibly click) using Python on OS X?

This is just for rapid prototyping, it doesn't have to be elegant.

like image 714
Ben Avatar asked Nov 11 '08 15:11

Ben


People also ask

Can you control your mouse with Python?

This code uses moveTo() function, which takes x and y coordinates, and an optional duration argument. This function moves your mouse pointer from it's current location to x, y coordinate, and takes time as specified by duration argument to do so.


2 Answers

Try the code at this page. It defines a couple of functions, mousemove and mouseclick, which hook into Apple's integration between Python and the platform's Quartz libraries.

This code works on 10.6, and I'm using it on 10.7. The nice thing about this code is that it generates mouse events, which some solutions don't. I use it to control BBC iPlayer by sending mouse events to known button positions in their Flash player (very brittle I know). The mouse move events, in particular, are required as otherwise the Flash player never hides the mouse cursor. Functions like CGWarpMouseCursorPosition will not do this.

from Quartz.CoreGraphics import CGEventCreateMouseEvent from Quartz.CoreGraphics import CGEventPost from Quartz.CoreGraphics import kCGEventMouseMoved from Quartz.CoreGraphics import kCGEventLeftMouseDown from Quartz.CoreGraphics import kCGEventLeftMouseUp from Quartz.CoreGraphics import kCGMouseButtonLeft from Quartz.CoreGraphics import kCGHIDEventTap  def mouseEvent(type, posx, posy):         theEvent = CGEventCreateMouseEvent(                     None,                      type,                      (posx,posy),                      kCGMouseButtonLeft)         CGEventPost(kCGHIDEventTap, theEvent)  def mousemove(posx,posy):         mouseEvent(kCGEventMouseMoved, posx,posy);  def mouseclick(posx,posy):         # uncomment this line if you want to force the mouse          # to MOVE to the click location first (I found it was not necessary).         #mouseEvent(kCGEventMouseMoved, posx,posy);         mouseEvent(kCGEventLeftMouseDown, posx,posy);         mouseEvent(kCGEventLeftMouseUp, posx,posy); 

Here is the code example from above page:

############################################################## #               Python OSX MouseClick #       (c) 2010 Alex Assouline, GeekOrgy.com ############################################################## import sys try:         xclick=intsys.argv1         yclick=intsys.argv2         try:                 delay=intsys.argv3         except:                 delay=0 except:         print "USAGE mouseclick [int x] [int y] [optional delay in seconds]"         exit print "mouse click at ", xclick, ",", yclick," in ", delay, "seconds" # you only want to import the following after passing the parameters check above, because importing takes time, about 1.5s # (why so long!, these libs must be huge : anyone have a fix for this ?? please let me know.) import time from Quartz.CoreGraphics import CGEventCreateMouseEvent from Quartz.CoreGraphics import CGEventPost from Quartz.CoreGraphics import kCGEventMouseMoved from Quartz.CoreGraphics import kCGEventLeftMouseDown from Quartz.CoreGraphics import kCGEventLeftMouseDown from Quartz.CoreGraphics import kCGEventLeftMouseUp from Quartz.CoreGraphics import kCGMouseButtonLeft from Quartz.CoreGraphics import kCGHIDEventTap def mouseEventtype, posx, posy:         theEvent = CGEventCreateMouseEventNone, type, posx,posy, kCGMouseButtonLeft         CGEventPostkCGHIDEventTap, theEvent def mousemoveposx,posy:         mouseEventkCGEventMouseMoved, posx,posy; def mouseclickposx,posy:         #mouseEvent(kCGEventMouseMoved, posx,posy); #uncomment this line if you want to force the mouse to MOVE to the click location first (i found it was not necesary).         mouseEventkCGEventLeftMouseDown, posx,posy;         mouseEventkCGEventLeftMouseUp, posx,posy; time.sleepdelay; mouseclickxclick, yclick; print "done." 
like image 200
Mike Rhodes Avatar answered Sep 23 '22 06:09

Mike Rhodes


The pynput library seems like the best currently maintained library. It allows you to control and monitor input devices.

Here is the example for controlling the mouse:

from pynput.mouse import Button, Controller  mouse = Controller()  # Read pointer position print('The current pointer position is {0}'.format(     mouse.position))  # Set pointer position mouse.position = (10, 20) print('Now we have moved it to {0}'.format(     mouse.position))  # Move pointer relative to current position mouse.move(5, -5)  # Press and release mouse.press(Button.left) mouse.release(Button.left)  # Double click; this is different from pressing and releasing # twice on Mac OSX mouse.click(Button.left, 2)  # Scroll two steps down mouse.scroll(0, 2) 
like image 42
GJ. Avatar answered Sep 21 '22 06:09

GJ.