Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mouse cursor position in C on multi screen system

Tags:

c++

c

linux

x11

mouse

how can I set the mouse cursor in an X window in C under Linux with multi screens? I have 2 monitors (with different resolution) plugged to a single linux pc. I used ":0.1" to address the second monitor. I run the application from monitor 1 keeping the mouse on the monitor 1 as well....as result mouse moves but don't jump on monitor 2. If I manually put the mouse cursor on monitor 2 and run the application from monitor 1, mouse moves.

I need a way to move the cursor between monitors.

#include "Xlib.h"
int main() {
  int delta_x = 5, delta_y = 5;
  Display *display = XOpenDisplay(":0.1");
  // move pointer relative to current position
  XWarpPointer(display, None, None, 0, 0, 0, 0, delta_x, delta_y);
  XCloseDisplay(display);
}
like image 256
Luke Avatar asked Dec 08 '10 08:12

Luke


1 Answers

You need to pass the handle of the root window of the display you want the pointer to move to:

    root = RootWindow(display, screennumber);
    XWarpPointer(display, None, root, 0, 0, 0, 0, x, y);

There is a complete working C example here:

http://www.ishiboo.com/~danny/Projects/xwarppointer/

which may be of use :)

like image 136
psmears Avatar answered Sep 18 '22 23:09

psmears