views:

1057

answers:

6

Hi all, 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();
}
+1  A: 

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
A: 

You'll need to use ncurses to do this: http://www.gnu.org/software/ncurses/ncurses.html

Macmade
+8  A: 

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
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
+1  A: 

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
+2  A: 

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
+3  A: 

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;}
Achernar
Oh right! Thank you :) I'll update my answer.
Bertrand Marron