views:

256

answers:

2

I need to monitor and filter mouse events with Xlib in Python.

So far I have found out that this code receives events, but does not pass them on, so I can't actually do anything with the mouse anymore.

from Xlib.display import Display
from Xlib import X

display = Display(':0')
root = display.screen().root

root.grab_pointer(True, X.ButtonPressMask | X.ButtonReleaseMask, X.GrabModeAsync, X.GrabModeAsync, 0, 0, X.CurrentTime)

while True:
    print "Event:"
    print display.next_event()

Alternatives I found are using

root.change_attributes(event_mask=X.ButtonPressMask | X.ButtonReleaseMask)

Which does not work at all or using the RECORD extension to Xlib, which I can't figure out how it works.

A: 

The answer seemed to be to use Xlib with RECORD, the result can be seen here: http://github.com/pepijndevos/PyMouse/blob/master/unix.py#L38

Pepijn
A: 

The link was broken. I think this is the latest one: http://github.com/pepijndevos/PyMouse/blob/master/pymouse/unix.py Line 58