22

how can I set the mouse cursor position in an X window using a C program under Linux? thanks :) (like setcursorpos() in WIN)

EDIT: I've tried this code, but doesn't work:

#include <curses.h>

main(){
 move(100, 100);
 refresh();
}
Calmarius
  • 18,570
  • 18
  • 110
  • 157
frx08
  • 4,222
  • 8
  • 37
  • 45
  • 3
    Your cursor position...in what? An X window? A terminal window? vi? – T.J. Crowder Mar 12 '10 at 14:47
  • in an X window.. but I don't have to get the cursor position, I have to set it everywhere in the screen – frx08 Mar 12 '10 at 14:48
  • (I've edited your question for you; you really wanted to do that when you replied to my comment.) You see the value of being specific. :-) You now have three answers completely unrelated to the question (they're all about setting cursor position in terminal windows). – T.J. Crowder Mar 12 '10 at 14:57
  • thanks but I didn't know if I explain myself, I need to move mouse programmaticly – frx08 Mar 12 '10 at 15:07
  • In X Window System, the mouse _cursor_ refers to the graphic icon, whereas you seem to want to move the _pointer_ itself (which moves the "hotspot" as well as also moving the cursor icon). – mctylr Mar 12 '10 at 15:28
  • Edited title to refer to the mouse cursor. (I stumbled upon this while searching for setting the terminal cursor position.) – Calmarius Feb 12 '14 at 17:53

6 Answers6

33

12.4 - Moving the Pointer

Although movement of the pointer normally should be left to the control of the end user, sometimes it is necessary to move the pointer to a new position under program control.

To move the pointer to an arbitrary point in a window, use XWarpPointer().


Example:

Display *dpy;
Window root_window;

dpy = XOpenDisplay(0);
root_window = XRootWindow(dpy, 0);
XSelectInput(dpy, root_window, KeyReleaseMask);
XWarpPointer(dpy, None, root_window, 0, 0, 0, 0, 100, 100);
XFlush(dpy); // Flushes the output buffer, therefore updates the cursor's position. Thanks to Achernar.
Bertrand Marron
  • 21,501
  • 8
  • 58
  • 94
  • thanks but it doesn't work: I edited the 2nd argument of XWarpPointer from NULL to None, I get no errors while compiling, but cursor doesn't move – frx08 Mar 12 '10 at 16:14
  • It didn't compile for me either and I think it's because of missing libraries. I tried including the libraries mentioned by @Achernar, but my compiler still can't find the references to XOpenDisplay and the other library calls. I'm including them as follows (obviously, not all in one line, but I'm having trouble formatting my comment): `#include #include #include #include ` Any ideas as to what's wrong here? – Seanny123 Jul 22 '13 at 02:49
  • 1
    Turns out you have to make sure to link the X library while compiling. `gcc text.cpp -lX11 -o t` – Seanny123 Jul 22 '13 at 03:31
14

This is old, but in case someone else comes across this issue. The answer provided by tusbar was correct but the command XFlush(dpy) must be added at the end to update the cursor's position. The libraries needed are: X11/X.h, X11/Xlib.h, X11/Xutil.h.

int main(int argc, char *argv[]){
         //Get system window
         Display *dpy;
         Window root_window;

         dpy = XOpenDisplay(0);
         root_window = XRootWindow(dpy, 0);
         XSelectInput(dpy, root_window, KeyReleaseMask);

         XWarpPointer(dpy, None, root_window, 0, 0, 0, 0, 100, 100);

         XFlush(dpy);

         return 0;}

PS: You can use this command to build the code gcc main.c -lX11

Kaveh
  • 4,618
  • 2
  • 20
  • 33
Achernar
  • 141
  • 1
  • 2
5

You want to write a X11 program that uses the call XWarpPointer function to move the point to a relative or global position. (Xlib Programming Manual, Vol 1)

In general, using Xlib for programming the X Window System, is the most basic, and quite low-level interface for graphical programming on a Unix or Linux system. Most applications developed nowadays using a higher level library such as GTK or Qt for developing their GUI applications.

Curses or NCurses (New Curses) is for programming terminal-oriented interfaces, so are not useful in this case.

mctylr
  • 5,159
  • 20
  • 32
4

use Jordan Sissel's excellent utility xdotool.

http://www.semicomplete.com/projects/xdotool/

it provide XWarpPointer wrapper function like xdo_mousemove(), here is some example:

Display *display = NULL;
xdo_t *xdo = NULL;

void mouse_left_down(int x, int y)
{
  xdo_mousemove(xdo, x, y, 0)
  xdo_mousedown(xdo, CURRENTWINDOW, Button1); 
}

void mouse_left_up(int x, int y)
{
  xdo_mouseup(xdo, CURRENTWINDOW, Button1, 1, 0); 
}

void mouse_left_double_click(int x, int y)
{
  xdo_mousemove(xdo, x, y, 0);
  xdo_click_multiple(xdo, CURRENTWINDOW, Button1, 1, 0);
  doubleclick = TRUE;
}

int main()
{

  display = XOpenDisplay(NULL);
  if(display == NULL)
  {
    fprintf(stderr, "can't open display!\n");
    return -1;
  }
  xdo = xdo_new((char*) display);

  //some task here
  // ...

  return 0;
}
plan9assembler
  • 2,862
  • 1
  • 24
  • 13
3

You can use XWarpPointer to move the mouse cursor in an X window.

XWarpPointer(display, src_w, dest_w, src_x, src_y, src_width, src_height, dest_x, 
                dest_y)
        Display *display;
        Window src_w, dest_w;
        int src_x, src_y;
        unsigned int src_width, src_height;
        int dest_x, dest_y;
Blindy
  • 65,249
  • 10
  • 91
  • 131
2

All modern terminals should support ANSI escape sequences. For anything more complicated (and more portable), however, you should look into using a library such as ncurses.

aib
  • 45,516
  • 10
  • 73
  • 79