我正在尝试欺骗击键;更精确一点:我正在重播一些应该在特定时间发送的击键 - 有时同时发送几个(或至少作为尽可能合理地靠在一起)。
使用 XTestFakeKeyEvent 实现这个,我遇到了一个问题。虽然到目前为止我所写的内容大部分都按预期工作并在正确的时间发送事件,但有时其中一些会失败。 XTestFakeKeyEvent 永远不会返回零(这表明失败),但这些事件似乎永远不会到达我试图将它们发送到的应用程序。我怀疑这可能是由于调用频率太高(有时超过 100+/秒),因为当每秒有大量击键时它看起来更容易失败。
一个小程序来说明我在做什么,为了简洁起见,不完整且没有错误检查:
// #includes ...
struct action {
int time; // Time where this should be executed.
int down; // Keydown or keyup?
int code; // The VK to simulate the event for.
};
Display *display;
int nactions; // actions array length.
struct action *actions; // Array of actions we'll want to "execute".
int main(void)
{
display = XOpenDisplay(NULL);
nactions = get_actions(&actions);
int cur_time;
int cur_i = 0;
struct action *cur_action;
// While there's still actions to execute.
while (cur_i < nactions) {
cur_time = get_time();
cur_action = actions + cur_i;
// For each action that is (over)due.
while ((cur_action = actions + cur_i)->time <= cur_time) {
cur_i++;
XTestFakeKeyEvent(display, cur_action->code,
cur_action->down, CurrentTime);
XFlush(display);
}
// Sleep for 1ms.
nanosleep((struct timespec[]){{0, 1000000L}}, NULL);
}
}
我意识到上面的代码非常适合我的情况,但我怀疑这是一个更广泛的问题 - 这也是我在这里提出这个问题的原因。
您可以/应该刷新 XEvents 的频率是否有限制?我发送的应用程序可能是问题所在,可能没有足够快地阅读它们吗?
最佳答案
已经有一段时间了,但经过一些修补后,结果证明我在按下和按下键之间的延迟太短了。将其设置为 15 毫秒后,应用程序正确地将操作注册为击键,并且(仍然)具有非常高的准确性。
回想起来我觉得有点傻,但我确实觉得这可能是其他人也会被绊倒的事情。
关于x11 - XTestFakeKeyEvent 调用被吞没,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51177438/