Simulating a mouse click

Peter Hutterer peter.hutterer at who-t.net
Wed Nov 2 16:34:32 PDT 2011


On Wed, Nov 02, 2011 at 03:27:42PM +0100, Markus Kramer wrote:
> 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.

"event" is the window the event happens on. once you send a button down on a
menu, the client usually grabs the device that sent the event and the event
window from then on is the grab window, not necessarily the window
underneath the cursor.

If you're tyring to emulate click events, I recommend to use the XTest
extension instead, it's much simpler to handle.

Cheers,
  Peter

> 
> 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
> _______________________________________________
> xorg at lists.freedesktop.org: X.Org support
> Archives: http://lists.freedesktop.org/archives/xorg
> Info: http://lists.freedesktop.org/mailman/listinfo/xorg
> Your subscription address: peter.hutterer at who-t.net
> 



More information about the xorg mailing list