Catch mouse clicks in python
Question: I'm trying to catch mouse click events in my python code (not just clicks in a
`Gtk` window I created but anywhere in the screen). After some search, I found
a thread talking about `xlib` (great, a dependency I already had).

    
    
    from Xlib import display, X
    
    display = display.Display()
    root = display.screen().root
    root.change_attribute(event_mask = X.ButtonPressMask | X.ButtonReleaseMask)
    
    while True:
        event = root.display.next_event()
        print(event)
    

The problem is that this chunk of code is throwing an error and I just can't
find good documentation...

    
    
    X protocol error:
    <class 'Xlib.error.BadAccess'>: code = 10, resource_id = 146, sequence_number = 9, major_opcode = 2, minor_opcode = 0
    

If you have any idea for this error or another way do this, it'll help really!


Answer: `BadAccess` here is because "An attempt is made to select an event type that
only one client can select at a time when another client has already selected
it."

From [x11 protocol
documentation](http://www.x.org/releases/X11R7.6/doc/xproto/x11protocol.txt),
ChangeWindowAttributes request (opcode 2 is this request code, resource id=146
is your root win id):

> Multiple clients can select input on the same window; their event-masks are
> disjoint. When an event is generated, it will be reported to all interested
> clients. However, only one client at a time can select for
> SubstructureRedirect , only one client at a time can select for
> ResizeRedirect, and only one client at a time can select for ButtonPress. An
> attempt to violate these restrictions results in an Access error.

Try to select `ButtonReleaseMask` only as a possible solution

