Mouse control with ctypes in OS X

Robert Kern robert.kern at
Mon Aug 20 21:23:49 CEST 2007

Paul McNett wrote:
> Niklas Ottosson wrote:
>> I need to get hold of the mouse position and also need to be able to 
>> change it. In windows I have used ctypes.windll.user32.getCursorPos() 
>> and ctypes.windll.user32.setCursorPos() with great success in my program 
>> but now I also need to make a Mac OS X version of the program.
>> Does anyone know what the above libraries and functions are called in OS 
>> X? Any help is greatly appreciated. 
> All I can offer you is the knowledge that OS X will probably not let you 
> set the mouse position, as it is a gross violation of the HIG (The user 
> is in charge of where the mouse appears, not the application).

Of course, sometimes you are not writing an application but a program that lets
the user interact with the computer in a different way to enable assistive
devices or just new hardware.

There are undocumented (near as I can tell) functions in IOKit for posting HID
events, including setting the mouse location. In
/System/Library/Frameworks/IOKit.framework/Headers/hidsystem/IOHIDLib.h there
are the functions IOHIDPostEvent() and IOHIDSetMouseLocation(). I haven't found
any documentation on these, but Google does find some code using them.

Googling around a bit more, however, I see that the Quartz Event Services is
documented and provides similar functionality. The DarwiinRemote project has a
mouse emulation mode for the Nintendo Wii controller that uses this API. It's
only available in OSX 10.4, however.

Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

More information about the Python-list mailing list