Skip to content
pepijndevos edited this page Sep 13, 2010 · 1 revision

This uses a few deprecated methods, but works for now.

Importing Quartz gives us straight access to many objC methods, without the fiddling with mappings I’ve seen al over the web.
NSEvent is the way to go in the future, but is currently only used to fetch the mouse position.

from Quartz import CGPostMouseEvent, CGWarpMouseCursorPosition, CGDisplayPixelsHigh, CGDisplayPixelsWide
from AppKit import NSEvent

Whenever I use (x, y) that means an NSPoint is required, but is mapped for us.
The first 1 is for telling if we should update the visual cursor.
The 3 is for the number of mouse buttons.
The others are the respective mouse buttons and their state.
You need to put all those back to 0 for releasing.

CGPostMouseEvent((x, y), 1, 3, 1, 0, 0)

This should be clear…

CGWarpMouseCursorPosition((x, y))

Just like this, the number is only important if you’re working with multiple displays.

CGDisplayPixelsWide(0)
CGDisplayPixelsHigh(0)

There is a pitfall with this one though, for historical reasons it gets the position from the left bottom of the screen, se we need the screen height to correct that:

loc = NSEvent.mouseLocation()
loc.x
CGDisplayPixelsHigh(0) - loc.y
Clone this wiki locally