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

Mac events use the same packages and event masks as Mac - new

First you have to create an event tap:

tap = CGEventTapCreate(
kCGSessionEventTap,
kCGHeadInsertEventTap,
kCGEventTapOptionDefault,
mask,
handler,
None)

Don’t ask me about the parameters, I don’t know. What I do know is that handler is a Python function that is called when an event occurs.

Mask is a set of event masks as listed in Mac - new wrapped in CGEventMaskBit and chained together with an or symbol:

CGEventMaskBit(kCGEventMouseMoved) | CGEventMaskBit(kCGEventLeftMouseDown) | CGEventMaskBit(kCGEventLeftMouseUp) etc.

Now we need another set of loops, taps, sources and what more…

loopsource = CFMachPortCreateRunLoopSource(None, tap, 0)
loop = CFRunLoopGetCurrent()
CFRunLoopAddSource(loop, loopsource, kCFRunLoopDefaultMode)
CGEventTapEnable(tap, True)

Then run it with
CFRunLoopRun()

or
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 5, False)

A good source for more information is the Apple developer documentation, they’ll be in the search result when you google for one of these function names.
Clone this wiki locally