Mouse button simulation - right button and context menu problem
I am programming remote touchpad. So I need to simulate mouse button press on Linux in C++. I use Xlib functions to do that.
Both touchpad buttons works almost fine, but for example when I press right button on the Linux desktop, context menu shows up and then it's impossible to choose anything from it by left button press simulation. I can't even get rid of that context menu by clicking outside of it.
Can you someone help me how to make my simulated touchpad buttons behave like real ones, please?
Here is my code:
void MainWindow::mouseClick(int button)
{
// Open X display
Display *display = XOpenDisplay (NULL);
if (display == NULL)
{
cerr << "Can't open display!" << endl;
}
// Create and setting up the event
XEvent event;
memset (&event, 0, sizeof (event));
event.xbutton.button = button;
event.xbutton.same_screen = True;
XQueryPointer (display, RootWindow(display, DefaultScreen(display)),
开发者_JAVA技巧 &event.xbutton.root, &event.xbutton.subwindow,
&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);
}
// Press
event.type = ButtonPress;
if (XSendEvent (display, PointerWindow, True, 0xfff, &event) == 0) cerr << "Error to send the event!" << endl;
XFlush (display);
usleep (1);
// Release
event.type = ButtonRelease;
event.xbutton.state = 0x100;
if (XSendEvent (display, PointerWindow, True, 0xfff, &event) == 0) cerr << "Error to send the event!" << endl;
XFlush (display);
usleep (1);
XCloseDisplay (display);
}
I faced similar problem while dealing with Xlib. I finally ended up using Xtest library. It is pretty simple. Just install libxtst on your linux box.
XTestFakeMotionEvent(display, -1, x, y, CurrentTime );
XTestFakeButtonEvent(display, button, True, CurrentTime); //Mouse down
XTestFakeButtonEvent(display, button, False, CurrentTime); //Mouse up
You need not worry about the windows hierarchy at all!
精彩评论