Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

xlib

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().

like image 591
Ray Andrews Avatar asked Jan 07 '12 05:01

Ray Andrews


2 Answers

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.

like image 149
Ray Andrews Avatar answered Oct 05 '22 11:10

Ray Andrews


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.

like image 21
R.. GitHub STOP HELPING ICE Avatar answered Oct 05 '22 09:10

R.. GitHub STOP HELPING ICE