私はx11経由でubuntu用の簡単なマウスクリッカーを書こうとしています。 (Mozillaはあまりにも正常に動作して)XTestFakeButtonEventとXSendEventの違い
#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);
}
このコードは、クロムを除くすべてのアプリケーション上で正常に動作します:
は、最初のiについては、クリックする手順の(XSendEventに基づいて)最初の変種を書きました。だから私は(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);
}
をこのコードは、罰金everyvereはクロムが含まれる作品。これらの機能の
通話は
// XSendEvent variant
mouseClick(1);
// XTestFakeButtonEvent variant
SendClick(1, true); // press lmb
SendClick(1, false); // release lmb
1非常に簡単です:私が間違ってやっているかを理解するために私を助ける最初の変種で(または多分クロムで何が間違って)。
1 1.1:私はXOpenDisplay(NULL)で表示を開くときに、必要なウィンドウではなくイベントを送信しようとしていると思います。 chromeはx11サーバーとは異なる接続システムを持っていますか?
2:アプリケーションで第2の亜種を使用することをお勧めしますか?それはかなり短く、私が持っているすべてのアプリでうまくいく)
P.S.このコードをコンパイルするには、-lX11 -lXtst libsを追加する必要があります。
Thx man。私はクロムがアンチクリッカープロテクションを持っていることを理解しており、XSendEventはアクティブなウィンドウだけでなく、どのウィンドウに対してもクリックを送信できるので、ユーザーはこれを見ることができないので、非アクティブなウィンドウでクリックを行うことができます) 私はxdotoolのソースコードを見ています。それ以外の場合は、CURRENTWINDOWおよびXSendEventにクリックを送信すると、_xdo_mousebuttonはXTestFakeButtonEventを使用します。 – goldstar