XTestFakeKeyEvent 调用被吞没
XTestFakeKeyEvent calls get swallowed
我正在尝试欺骗击键;更精确一点:我正在重播一些击键,这些击键应该在特定时间全部发送 - 有时会同时发送几个时间(或至少尽可能合理地靠近)。
使用 XTestFakeKeyEvent 实现这个,我遇到了一个问题。虽然到目前为止我所写的内容大部分都按预期工作并在正确的时间发送事件,但有时其中一些会失败。 XTestFakeKeyEvent 永远不会 returns 零(这表示失败),但这些事件似乎永远不会到达我试图将它们发送到的应用程序。我怀疑这可能是由于调用频率太高(有时 100+/秒),因为当有大量 keystrokes/second.
时看起来更容易失败
一个小程序来说明我在做什么,为了简洁起见,不完整且没有错误检查:
// #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);
}
}
我意识到上面的代码非常适合我的情况,但我怀疑这是一个更广泛的问题 - 这也是我在这里提问的原因。
您 can/should 刷新 XEvents 的频率是否有限制?我发送的应用程序是否有问题,也许没有足够快地阅读它们?
已经有一段时间了,但经过一些修补后,我发现我在按下和向上键之间的延迟太短了。将其设置为 15 毫秒后,应用程序正确地将操作注册为击键,并且(仍然)具有非常高的准确性。
回想起来我觉得有点傻,但我确实觉得这可能是其他人也会被绊倒的事情。
我正在尝试欺骗击键;更精确一点:我正在重播一些击键,这些击键应该在特定时间全部发送 - 有时会同时发送几个时间(或至少尽可能合理地靠近)。
使用 XTestFakeKeyEvent 实现这个,我遇到了一个问题。虽然到目前为止我所写的内容大部分都按预期工作并在正确的时间发送事件,但有时其中一些会失败。 XTestFakeKeyEvent 永远不会 returns 零(这表示失败),但这些事件似乎永远不会到达我试图将它们发送到的应用程序。我怀疑这可能是由于调用频率太高(有时 100+/秒),因为当有大量 keystrokes/second.
时看起来更容易失败一个小程序来说明我在做什么,为了简洁起见,不完整且没有错误检查:
// #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);
}
}
我意识到上面的代码非常适合我的情况,但我怀疑这是一个更广泛的问题 - 这也是我在这里提问的原因。
您 can/should 刷新 XEvents 的频率是否有限制?我发送的应用程序是否有问题,也许没有足够快地阅读它们?
已经有一段时间了,但经过一些修补后,我发现我在按下和向上键之间的延迟太短了。将其设置为 15 毫秒后,应用程序正确地将操作注册为击键,并且(仍然)具有非常高的准确性。
回想起来我觉得有点傻,但我确实觉得这可能是其他人也会被绊倒的事情。