How to set mouse cursor position in C on linux?

后端 未结 6 1103
礼貌的吻别
礼貌的吻别 2020-12-14 07:52

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

6条回答
  •  没有蜡笔的小新
    2020-12-14 08:43

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

提交回复
热议问题