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

点点圈 提交于 2019-12-10 03:12:33

问题


I want to find C / Xorg code to 'enter' a left mouse button click. 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.

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. So, I want to build the 'click' into my code.

The code I tried that didn't work was based on XSendEvent().


回答1:


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.

Xlib seems to be so bloody difficult. I've had advice to use other libraries, I wish I knew how to go about changing over.

Thanks R.




回答2:


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.




回答3:


xdotool is the easy way of doing this. It's a command line tool. You can use it in simple scripts. For example:

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


来源:https://stackoverflow.com/questions/8767524/how-do-we-simulate-a-mouse-click-with-xlib-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!