Linux上按键模拟的问题(Ubuntu 18)

Problem with Keypress simulation on Linux (Ubuntu 18)

本文关键字:Ubuntu 问题 模拟 Linux      更新时间:2023-10-16

我正在制作一个业余爱好项目,它基本上是一个非常旧的闪光游戏的机器人,鼠标移动和点击正常工作,但是所有钥匙按下都使操作系统滞后/口吃和有时停止听所有键盘输入,即真实或假货。

我开始仅使用XTEST的Xlib,但没有起作用,所以我尝试了Xsendevent而不是XTEST,但是所有症状都保持不变,因此最后一次尝试是XDO,这给出了更好的结果,但仍然冻结了OS。

这是我要使用的当前片段来模拟按键:

//Constructor
CheatCore::CheatCore() {
    xdo_t x = xdo_new(NULL);
    Window *list;
    xdo_search_t search;
    unsigned int nwindows;
    memset(&search, 0, sizeof(xdo_search_t));
    search.max_depth = -1;
    search.require = xdo_search::SEARCH_ANY;
    search.searchmask = SEARCH_CLASS | SEARCH_ONLYVISIBLE;
    search.winclass = "Chrome";
    int id = xdo_search_windows(x, &search, &list, &nwindows);
    qDebug() << nwindows;
    if(!nwindows){
        qDebug() << "Chrome not found";
        return;
    }
    w = list[0];
    //I have to call activate twice to really bring it forward, I suspect that its
    //because I use a transparent "overlay" that show stats for the cheat and it is set as Aways on top
    //(i used Qt to set it to not get any Events)
    xdo_activate_window(x,w);
    xdo_activate_window(x,w);
}
//there is a function that executes every second to check if a pixel color has changed,
//if so, then the SendKey is called to Reload weapon magazine pressing the "space" key
void CheatCore::SendKey(){
    xdo_activate_window(x,w);
    xdo_activate_window(x,w);
    xdo_send_keysequence_window(x, w, "space", 500);
}

我正在使用透明覆盖层来显示bot状态,只出现了一些数字,它是使用qt创建的小部件,该小部件是 AlwaysOnTop,绘制事件绘制了所需的信息,它是另一个对象,没有在CheatCore中的直接影响,但这是用于绘制透明窗口并忽略事件的窗口标志。

setWindowFlags(Qt::WindowTransparentForInput | Qt::FramelessWindowHint | 
Qt::WindowStaysOnTopHint);
setAttribute(Qt::WA_NoSystemBackground);
setAttribute(Qt::WA_TranslucentBackground);
setAttribute(Qt::WA_TransparentForMouseEvents);

我没有设法理解什么可能引起这种怪异的行为,它可能是窗口系统吗?

另外,我尝试找到一种QT模拟鼠标/键盘输入的方法,但是如果有可能实现这一目标的方法,我没有设法找到任何解决方案将事件发送到其他窗口!

我要自动化的游戏称为" storm the House"

如果有兴趣,这是指向在线仓库的链接:link

您能帮我做这项工作吗?谢谢!

有关设置的上下文:Ubuntu 18.10使用VGA和NVIDIA驱动程序(如果可能影响XServer)

您是否曾经尝试使用命令行中的Xdotool。要使用Xdotool,您需要先安装软件包。要模拟按键按下,您可以使用。

xdotool key <key>

例如,如果要模拟按键x的键,则可以使用此代码

xdotool key x

或任何其他组合,例如

xdotool key ctrl+f

也可以用另一个键替换按键,例如,如果要用backspace替换d,则可以尝试此

xdotool key D BackSpace 

您可以在线阅读完整的GUID,也可以使用此工具编写脚本并在许多不同的情况下使用它。您也可以将其用于远程连接。

我希望这对您的小问题有所帮助。

使用evdev是Linux特定选项。

这是一个更简单的解决方案,因为您只需要打开正确的文件并写入它。

看看这个类似的问题,看看如何开始。