鼠标按键模拟-右键和上下文菜单问题
我正在对远程触摸板进行编程。所以我需要用C++模拟Linux上的鼠标按钮按下。我使用 Xlib 函数来做到这一点。
两个触摸板按钮都几乎可以正常工作,但例如,当我在 Linux 桌面上按下右键时,会显示上下文菜单,然后无法通过左键按下模拟从中选择任何内容。我什至无法通过单击外部菜单来摆脱该上下文菜单。
有人可以帮助我如何让我的模拟触摸板按钮表现得像真实的按钮吗?
这是我的代码:
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)),
&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 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)),
&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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在处理 Xlib 时遇到了类似的问题。我最终使用了 Xtest 库。这很简单。只需在您的 Linux 机器上安装 libxtst 即可。
您根本不必担心 Windows 层次结构!
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.
You need not worry about the windows hierarchy at all!