开发者

How to set mouse cursor position in C on linux?

开发者 https://www.devze.com 2022-12-22 14:36 出处:网络
how can I set the mou开发者_开发百科se cursor position in an X window using a C program under Linux?

how can I set the mou开发者_开发百科se 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();
}


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.


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


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.


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;
}


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;


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消