XTestFakeButtonEvent&之间的区别XSendEvent [英] Difference between XTestFakeButtonEvent & XSendEvent

查看:382
本文介绍了XTestFakeButtonEvent&之间的区别XSendEvent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过x11为ubuntu编写简单的鼠标点击器.

I'm trying to write simple mouse clicker for ubuntu via x11.

首先,我编写了点击程序的第一个变体(基于XSendEvent):

For first i wrote first variant (based on XSendEvent) of clicking procedure:

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

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

    XEvent event;

    if(display == NULL)
    {
        std::cout << "clicking error 0" << std::endl;
        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)
        std::cout << "clicking error 1" << std::endl;

    XFlush(display);

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

    if(XSendEvent(display, PointerWindow, True, 0xfff, &event) == 0)
        std::cout << "clicking error 2" << std::endl;

    XFlush(display);

    XCloseDisplay(display);
}

此代码在除chrome之外的所有应用程序上都可以正常工作(与mozilla一起工作也可以).

This code works fine on every application except chrome (with mozilla works fine too).

所以我写了第二个变种(基于XTestFakeButtonEvent):

So i wrote second variant (based on XTestFakeButtonEvent):

#include <X11/extensions/XTest.h>

void SendClick(int button, Bool down) 
{
    Display *display = XOpenDisplay(NULL);
    XTestFakeButtonEvent(display, button, down, CurrentTime);
    XFlush(display);
    XCloseDisplay(display);
}

并且此代码在包括chrome在内的所有代码中都可以正常工作.

And this code works fine everyvere include chrome.

调用这些函数非常简单

// XSendEvent variant
mouseClick(1);

// XTestFakeButtonEvent variant
SendClick(1, true);   // press lmb
SendClick(1, false);  // release lmb

1:帮助我了解我在第一个版本中做错了什么(或者Chrome中可能做错了).

1: help me to understand what i'm doing wrong (or what wrong in chrome maybe) in first variant.

1.1:当我使用XOpenDisplay(NULL);打开显示时,我认为我正在尝试发送事件而不是所需窗口的事件. chrome与x11服务器的连接系统是否不同?

1.1: I think that i'm trying to send event not for needed window, when i open display with XOpenDisplay(NULL);. Does chrome have different connection system with x11 server?

2:在应用程序中使用第二个变体是个好主意吗?它很短,并且可以与我拥有的每个应用程序正常工作

2: is it good idea to use second variant in applications? It pretty short and works fine with every app i have)

P.S.要编译此代码,您需要添加-lX11 -lXtst库

P.S. to compile this code you need add -lX11 -lXtst libs

推荐答案

XSendEvent生成标记为已发送的事件.服务器发送的事件未标记.

XSendEvent produces events that are marked as sent. Events sent by the server are not marked.

   typedef struct {
           int type;
           unsigned long serial; 
           Bool send_event;        // <----- here
           Display *display;
           Window window;
   } XAnyEvent;

出于安全原因,某些应用程序会忽略设置了此标志的事件.考虑恶意软件以某种方式可以访问您的X11服务器-通过发送这些事件,它可以欺骗任何应用程序执行其所需的操作.

Some applications ignore events that have this flag set, for security reasons. Think of malware that somehow gets access to your X11 server — it can trick any application into doing whatever it wants by sending those events.

在您自己的计算机上使用第二个变体是完全可以的,但是它依赖于可以禁用的扩展名(再次出于安全原因),因此不一定可以在其他人的X11服务器上工作.

It is perfectly OK to use the second variant on your own machine, but it relies on an extension that can be disabled (again, for security reasons) and so not necessarily works on other people's X11 servers.

这篇关于XTestFakeButtonEvent&amp;之间的区别XSendEvent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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