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

3

8

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

Differentiable answered 7/1, 2012 at 5:3 Comment(2)
+1 for including the reason you want to do this.Rosemarie
if you don't want to reach for the mouse you should consider using a window manager that supports this kind of things. I can recommend xmonad, but there are others.Luteolin
D
5

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.

Differentiable answered 8/1, 2012 at 4:18 Comment(0)
R
3

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.

Rosemarie answered 7/1, 2012 at 5:37 Comment(5)
R: The whole point of the excercise to to be able to move between screens w.o. having to touch the mouse, so if I can move the pointer via KB, but have to reach for the mouse to left click anyway, then the whole thing becomes a bit pointless. Of course there's no problem when moving between windows on the same screen, but when trying to move from one screen to the other, there is no built in shortcut that I can find.Differentiable
Move the mouse pointer as you're doing, but then send a raise/focus rather than clicking.Rosemarie
Good Idea! It seems Alt+Esc does it. Now I just need to find out how to send that.Differentiable
With bindkey " \e^[ " identifies Alt+Esc, now to translate that into something C can understand ...Differentiable
No, do not send a key event. That's a bogus hack that's specific to one window manager. Instead, After using the Xlib calls to reposition the mouse pointer, use Xlib to determine the window under the mouse pointer and raise/focus it with Xlib. There is no reason to ever generate fake events for what you're trying to do.Rosemarie
L
2

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
Luteolin answered 16/1, 2016 at 13:42 Comment(3)
Thanks, that's an interesting program, it looks better than my 'movemouse', tho mine is perhaps better for rock-bottom simple mouse moving.Differentiable
The op is asking for a 'C' based answer. Didn't you read the first post? It is marked a 'C'. Three people think this is a good answer for 'C'. Stackoverflow is really blurring the lines of programming.Meraz
my dear.. thanks for pointing that out. That was close. I hope nobody got hurt, trying to compile this answer with a c-compilerLuteolin

© 2022 - 2024 — McMap. All rights reserved.