Linux,如何捕获屏幕并模拟鼠标移动

基尔博:

我需要以某种方式捕获屏幕(作为打印屏幕),以便可以访问像素颜色数据,以进行一些图像识别,此后,我将需要在屏幕上生成鼠标事件,例如左键单击,拖放(移动鼠标)同时按下按钮,然后释放它)。完成后,图像将被删除。

注意:我需要捕获整个屏幕,用户可以看到所有内容,并且需要模拟程序窗口外的点击(如果有任何不同)

规格:Linux ubuntu语言:C ++

性能不是很重要,“打印屏幕”功能将每隔10秒执行一次。该过程的持续时间最多可为24小时,因此方法必须稳定且内存无泄漏(通常:)

我能够通过Win GDI和一些Windows事件在Windows中完成操作,但是我不知道如何在Linux中进行操作。

非常感谢

公理:
//sg

//Solution using Xlib for those who use Linux
#include <X11/Xlib.h>
#include<stdio.h>
#include<unistd.h>
#include <stdlib.h>
#include <string.h>

#include <unistd.h>

#include <X11/Xlib.h>
#include <X11/Xutil.h>

void mouseClick(int button)
{
    Display *display = XOpenDisplay(NULL);

    XEvent event;

    if(display == NULL)
    {
        fprintf(stderr, "Cannot initialize the display\n");
        exit(EXIT_FAILURE);
    }

    memset(&event, 0x00, sizeof(event));

    event.type = ButtonPress;
    event.xbutton.button = button;
    event.xbutton.same_screen = True;

    XQueryPointer(display, RootWindow(display, DefaultScreen(display)), &event.xbutton.root, &event.xbutton.window, &event.xbutton.x_root, &event.xbutton.y_root, &event.xbutton.x, &event.xbutton.y, &event.xbutton.state);

    event.xbutton.subwindow = event.xbutton.window;

    while(event.xbutton.subwindow)
    {
        event.xbutton.window = event.xbutton.subwindow;

        XQueryPointer(display, event.xbutton.window, &event.xbutton.root, &event.xbutton.subwindow, &event.xbutton.x_root, &event.xbutton.y_root, &event.xbutton.x, &event.xbutton.y, &event.xbutton.state);
    }

    if(XSendEvent(display, PointerWindow, True, 0xfff, &event) == 0) fprintf(stderr, "Error\n");

    XFlush(display);

    usleep(100000);

    event.type = ButtonRelease;
    event.xbutton.state = 0x100;

    if(XSendEvent(display, PointerWindow, True, 0xfff, &event) == 0) fprintf(stderr, "Error\n");

    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);
    XWarpPointer(display, None, root, 0, 0, 0, 0, x, y);
    mouseClick(Button1);
    XFlush(display);
    XCloseDisplay(display);
    return 0;
}

进行构建,然后模拟x上的单击,执行:

$ ./a.out x y

$ g ++ -lX11 sgmousesim2.cpp

$ ./a.out 123 13

以防万一您仍然感兴趣。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章