问题
i'm trying to send mouse click event using xlib in a ubuntu 12.04, all works when i do the click in the desktop bar icons and works when i do click in the title bar of each window (close, minimize, maximize window) but in some windows doing a click inside do not work, only work in my qt creator window but when i click in, for example, Home folder icon then move the mouse inside the folder, i can't do any click in folders or menu bar, only works in the title bar of the windows.
Maybe is a bug of Ubuntu Unity desktop? here is my code that i find on internet:
#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
void mouseClick(int button)
{
Display *display = XOpenDisplay(NULL);
XEvent event;
if(display == NULL)
{
fprintf(stderr, "Errore nell'apertura del Display !!!\n");
exit(EXIT_FAILURE);
}
memset(&event, 0x00, sizeof(event));
event.type = ButtonPress;
event.xbutton.button = button;
event.xbutton.same_screen = True;
XQueryPointer(display, RootWindow(display, DefaultScreen(display)), &event.xbutton.root, &event.xbutton.window, &event.xbutton.x_root, &event.xbutton.y_root, &event.xbutton.x, &event.xbutton.y, &event.xbutton.state);
event.xbutton.subwindow = event.xbutton.window;
while(event.xbutton.subwindow)
{
event.xbutton.window = event.xbutton.subwindow;
XQueryPointer(display, event.xbutton.window, &event.xbutton.root, &event.xbutton.subwindow, &event.xbutton.x_root, &event.xbutton.y_root, &event.xbutton.x, &event.xbutton.y, &event.xbutton.state);
}
if(XSendEvent(display, PointerWindow, True, 0xfff, &event) == 0) fprintf(stderr, "Errore nell'invio dell'evento !!!\n");
XFlush(display);
usleep(100000);
event.type = ButtonRelease;
event.xbutton.state = 0x100;
if(XSendEvent(display, PointerWindow, True, 0xfff, &event) == 0) fprintf(stderr, "Errore nell'invio dell'evento !!!\n");
XFlush(display);
XCloseDisplay(display);
}
My ubuntu is inside of a vmware player, i move the cursor using XWarpPointer of Xlib, thank you for any help.
回答1:
I had the same problem and I just solved it using Xext and Xtest extensions.
#include <X11/extensions/XTest.h>
int main(int argc, char ** argv)
{
XEvent event;
Display *dpy = XOpenDisplay (NULL);
/* Fake the mouse button Press and Release events */
XTestFakeButtonEvent (dpy, 1, True, CurrentTime);
XTestFakeButtonEvent (dpy, 1, False, CurrentTime);
XCloseDisplay (dpy);
return 0;
}
来源:https://stackoverflow.com/questions/14656615/xlib-xsendevent-click-event-do-not-work-inside-of-some-windows-on-ubuntu-12-04