Simulating a mouse click

Markus Kramer tomatonius at googlemail.com
Wed Nov 2 07:27:42 PDT 2011


Hi,
I'm attempting to use xlib to simulate a series of mouse clicks in an
application (gedit). I found some posts that describe how to do this
and it essentially works. I can do left clicks on buttons and so on.
But if I generate a click on the menu bar, a menu opens and something
breaks and no further ButtonPress/ButtonRelease events that I send
will be processed by the application.

When debugging it with xtrace I noticed that my events look slightly
different than real left click events.

Real:
ButtonPress(4) [1]button=left button(0x01) [4]time=0x03752355
[8]root=0x00000102 [12]event=0x010000a3 [16]child=None(0x00000000)
[20]root-x=428 [22]root-y=33 [24]event-x=295 [26]event-y=10 state=0
[30]same-screen=true(0x01)


My simulated event:
SendEvent [1]propagate=true(0x01)
[4]destination=PointerWindow(0x00000000) event-mask=ButtonPress
ButtonPress(4) [1]button=left button(0x01) [4]time=0x00000000
[8]root=0x00000102 [12]event=0x012000a3 [16]child=None(0x00000000)
[20]root-x=407 [22]root-y=40 [24]event-x=274 [26]event-y=19 state=0
[30]same-screen=true(0x01)


The field "event" of the real event says 0x010000a3 mine is
0x012000a3. What is this "event" field about? It is not part of the
ButtonPress struct.

This is the code that I use to simulate a left click:

---------------------------------------------------
XEvent event;
memset(&event, 0, sizeof(XEvent));

XWindowAttributes attr;
XGetWindowAttributes(dpy, windowId, &attr);
event.type = ButtonPress;
event.xbutton.same_screen = TRUE;
event.xbutton.root = root;
event.xbutton.window = windowId;
event.xbutton.subwindow = None;
event.xbutton.x = x;
event.xbutton.y = y;
event.xbutton.x_root = attr.x + x;
event.xbutton.y_root = attr.y + y;
event.xbutton.state = 0;
event.xbutton.button = Button1;

XSendEvent(dpy, PointerWindow, True, ButtonPressMask, &event);
XFlush(dpy);

// the same for ButtonRelease, with modified state and mask
--------------------------------------------------


Thanks in advance.
Markus



More information about the xorg mailing list