Linux, how to capture screen, and simulate mouse movements

前端 未结 4 954
春和景丽
春和景丽 2020-12-13 07:41

I need to capture screen (as print screen) in the way so I can access pixel color data, to do some image recognition, after that I will need to generate mouse events on the

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-13 08:23

    I couldn't get the clicking working with the method @axiom used, only movement of the pointer. I used this instead: (Ubuntu 18.04).

    Compiles with: g++ mouse_click.cpp -lX11 -lXtst -lstdc++

    #include
    #include
    #include 
    #include 
    
    #include 
    
    #include 
    #include 
    #include 
    
    
    void mouseClick(int button)
    {
        Display *display = XOpenDisplay(NULL);
    
        // click left button
        XTestFakeButtonEvent(display, Button1, true, 0);
        XFlush(display);
    
        usleep(10000);
    
        // release left mouse
        XTestFakeButtonEvent(display, Button1, false, 0);
        XFlush(display);
    
    
        XCloseDisplay(display);
    
    
    }
    int main(int argc,char * argv[]) {
    
        int x , y;
        x=atoi(argv[1]);
        y=atoi(argv[2]);
        Display *display = XOpenDisplay(0);
    
        Window root = DefaultRootWindow(display);
        XTestFakeMotionEvent(display, root, x, y, 0);
        XFlush(display);
        mouseClick(Button1);
        XFlush(display);
        XCloseDisplay(display);
        return 0;
    }
    

提交回复
热议问题