简体   繁体   English

我们如何用Xlib / C模拟鼠标点击?

[英]How do we simulate a mouse click with Xlib / C?

I want to find C / Xorg code to 'enter' a left mouse button click. 我想找到C / Xorg代码来“输入”鼠标左键单击。 I'd expect a single line of code but the only things I've found written in C are about two dozen lines long and they don't work anyway :( It seems it can be done in Windows, but I'm in Linux. 我期待一行代码,但我发现用C写的唯一的东西大约是二十几行而且它们无论如何都不起作用:(它似乎可以在Windows中完成,但我在Linux中。

The reason for the question is that I've written a utility that lets me move my mouse pointer between several screens using the keyboard. 问题的原因是我编写了一个实用程序,可以让我使用键盘在几个屏幕之间移动鼠标指针。 The only problem is that if I move to a location where window abc used to be but another window xyz has been loaded on top of that same location, the mouse pointer moves to xyz just fine, but xyz doesn't have focus -- until I left click the mouse. 唯一的问题是,如果我移动到一个窗口abc曾经的位置,但另一个窗口xyz已经加载到同一位置的顶部,鼠标指针移动到xyz就好了,但是xyz没有焦点 - 直到我左键单击鼠标。 So, I want to build the 'click' into my code. 所以,我想在我的代码中构建'click'。

The code I tried that didn't work was based on XSendEvent(). 我尝试过的代码不起作用是基于XSendEvent()。

Yes, I've more or less come to understand. 是的,我或多或少地明白了。 Anyway it seems this is the way: 无论如何,这似乎是这样的:

{
#include <X11/extensions/XTest.h>
XTestFakeButtonEvent(display, 1, True, CurrentTime);
XTestFakeButtonEvent(display, 1, False, CurrentTime);
XFlush(display);
}

... and add " -lXtst " to the LDFLAGS line in the Makefile. ...并将“-lXtst”添加到Makefile中的LDFLAGS行。

Xlib seems to be so bloody difficult. Xlib似乎非常难熬。 I've had advice to use other libraries, I wish I knew how to go about changing over. 我有建议使用其他库,我希望我知道如何改变。

Thanks R. 谢谢R.

Why not just directly raise/focus the window rather than trying to make a fake click event? 为什么不直接提升/聚焦窗口而不是试图制作假的点击事件? That should be a lot more reliable and work with all window managers, even non-click-to-focus ones. 这应该更加可靠,并且适用于所有窗口管理器,甚至是非点击关注窗口管理器。

xdotool is the easy way of doing this. xdotool是这样做的简单方法。 It's a command line tool. 这是一个命令行工具。 You can use it in simple scripts. 您可以在简单的脚本中使用它。 For example: 例如:

#!/bin/sh
xdotool mousemove x y
xdotool click 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM