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

jww
  • 97,681
  • 90
  • 411
  • 885
Ray Andrews
  • 532
  • 4
  • 14

3 Answers3

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.

Ray Andrews
  • 532
  • 4
  • 14
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
johannes_lalala
  • 561
  • 4
  • 12
  • Thanks, that's an interesting program, it looks better than my 'movemouse', tho mine is perhaps better for rock-bottom simple mouse moving. – Ray Andrews Jan 16 '16 at 22:16
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.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
  • 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. – Ray Andrews Jan 07 '12 at 17:00
  • Move the mouse pointer as you're doing, but then send a raise/focus rather than clicking. – R.. GitHub STOP HELPING ICE Jan 07 '12 at 21:29
  • Good Idea! It seems Alt+Esc does it. Now I just need to find out how to send that. – Ray Andrews Jan 07 '12 at 21:48
  • With bindkey " \e^[ " identifies Alt+Esc, now to translate that into something C can understand ... – Ray Andrews Jan 07 '12 at 22:01
  • 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. – R.. GitHub STOP HELPING ICE Jan 08 '12 at 03:42