Linux、画面をキャプチャし、マウスの動きをシミュレートする方法
-
25-09-2019 - |
質問
画面(印刷画面として)キャプチャする必要があるため、ピクセルのカラーデータにアクセスして画像認識を行うことができます。その後、左クリック、ドラッグ、ドロップなどの画面でマウスイベントを生成する必要があります(マウスの移動ボタンが押されている間、それをリリースします)。完了すると、画像が削除されます。
注:ユーザーが見ることができるすべての画面をキャプチャする必要があり、プログラムの外側のクリックをシミュレートする必要があります(違いがある場合)
仕様:Linux Ubuntu言語:C ++
パフォーマンスはそれほど重要ではありません。「印刷画面」機能は、10秒ごとに1回実行されます。プロセスの期間は最大24時間になる可能性があるため、方法は安定してメモリリークが無料である必要があります(usuall :)
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でクリックをシミュレートするには、y do:
$ ./a.out x y
すなわち
$ g ++ -lx11 sgmouseSim2.cpp
$ ./A.out 123 13
あなたがまだ興味を持っている場合に備えて。
他のヒント
スワインプット マウス/キーイベントをシミュレートするためのソリューションです。おそらくカーネル用にコンパイルする必要があります。 Xorgは、マウス/キーイベントを録音するためのヘッダーをいくつか提供しましたが、現時点では壊れていると思います。があります C
コード evtest それはからのイベントをキャプチャするために使用できます /dev/input/eventX
, /dev/input/mice
ファイル。役立つことがあります。
編集:
バグが修正されました Xorgレコード拡張機能では、動作している可能性があります。
正しく動作するには、ポインターをゆがめた後はXflushに電話する必要があります
Linux Mint19(Cinamon 3.8)xwarppointer(display、none、root、0、0、0、0、x、y)でテストしました。 Xflush(display);
使用したメソッド@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;
}