Linux,如何捕获屏幕,并模拟鼠标移动 [英] Linux, how to capture screen, and simulate mouse movements

查看:738
本文介绍了Linux,如何捕获屏幕,并模拟鼠标移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 screen such as left click, drag and drop (moving mouse while button is pressed, and then release it). Once its done, image will be deleted.

注意:我需要捕获整个屏幕用户可以看到的一切,我需要模拟我的程序窗口外的点击(如果有什么区别)

Note: I need to capture whole screen everything that user can see, and I need to simulate clicks outside window of my program (if it makes any difference)

规格:Linux ubuntu
语言:C ++

Spec: Linux ubuntu Language: C++

性能不是很重要,打印屏幕每〜10秒执行一次。
进程的持续时间可以长达24小时,因此方法需要稳定,内存泄漏(usuall:)

Performance is not very important,"print screen" function will be executed once every ~10 sec. Duration of the process can be up to 24 hours so method needs to be stable and memory leaks free (as usuall :)

我能做到窗口与赢得GDI和一些Windows事件,但我不知道如何在Linux中做它。

I was able to do in windows with win GDI and some windows events, but I'ev no idea how to do it in 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;
}


$ b p>

Build it and then to simulate a click at x ,y do:

$ ./a.out x y

$ g ++ -lX11 sgmousesim2.cpp

$ ./a.out 123 13

只是为了防止您仍感兴趣。

Just in case you are still interested.

这篇关于Linux,如何捕获屏幕,并模拟鼠标移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆