Simulating a mouse click
markus.kramer at rocketmail.com
Fri Nov 4 04:21:47 PDT 2011
Thanks for the help.
I was able to fix this now by setting the time member correctly. I had
it set to CurrentTime before, but that didn't fix it.
On 03.11.2011 00:34, Peter Hutterer wrote:
> On Wed, Nov 02, 2011 at 03:27:42PM +0100, Markus Kramer wrote:
>> 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.
>> ButtonPress(4) button=left button(0x01) time=0x03752355
>> root=0x00000102 event=0x010000a3 child=None(0x00000000)
>> root-x=428 root-y=33 event-x=295 event-y=10 state=0
>> My simulated event:
>> SendEvent propagate=true(0x01)
>> destination=PointerWindow(0x00000000) event-mask=ButtonPress
>> ButtonPress(4) button=left button(0x01) time=0x00000000
>> root=0x00000102 event=0x012000a3 child=None(0x00000000)
>> root-x=407 root-y=40 event-x=274 event-y=19 state=0
>> 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.
>> 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);
>> // the same for ButtonRelease, with modified state and mask
>> Thanks in advance.
>> 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