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

15
我需要截取屏幕(如打印屏幕),以便能够访问像素颜色数据,进行图像识别。之后我需要在屏幕上生成鼠标事件,例如左键单击、拖放(按住鼠标按钮移动鼠标,然后释放它)。完成后,将删除图像。
注:我需要捕获用户可见的整个屏幕,并且需要模拟程序窗口外的点击(如果有任何区别)。
规格:Linux Ubuntu 语言:C ++
性能不是非常重要,“打印屏幕”功能将每隔约10秒执行一次。过程的持续时间最长可达24小时,因此方法必须稳定且无内存泄漏(通常情况下都是如此)。
我在Windows中使用win GDI和一些Windows事件做到了这一点,但我不知道如何在Linux中实现。
非常感谢。

1
这可能与X服务器(X11)有关。 - ereOn
4个回答

23
//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,y 处单击:

$ ./a.out x y

i.e.

$ g++ -lX11 sgmousesim2.cpp

$ ./a.out 123 13

仅供参考,以防你仍然感兴趣。


你能解释一下这行代码 mouseClick(Button1); 吗?Button1 是什么?我是Java开发者,不太理解它。 - Pavel_K

1

Swinput 是模拟鼠标/键盘事件的解决方案。您需要为您的内核编译它。Xorg提供了一些记录鼠标/键盘事件的头文件,但我认为目前它已经损坏了。有一个C代码evtest可以用来捕获/dev/input/eventX/dev/input/mice文件中的事件。这可能会有所帮助。

编辑:

错误已经被修复在Xorg记录扩展中,所以它也可能正常工作。


1

我无法使用@axiom所用的方法使点击生效,只能移动指针。我改用了这个方法:(Ubuntu 18.04)。

编译命令:g++ mouse_click.cpp -lX11 -lXtst -lstdc++

#include<stdio.h>
#include<unistd.h>
#include <stdlib.h>
#include <string.h>

#include <unistd.h>

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


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

0
为了使其正常工作,您必须在移动指针后调用XFlush。
在Linux Mint19(Cinamon 3.8)上进行了测试 XWarpPointer(display,None,root,0,0,0,0,x,y); XFlush(display);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接