6

I try to send key to an application with XLib and XTestFakeKeyEvent, and it works fine, with the following code :

XSetInputFocus(disp, list[selectWindow],RevertToPointerRoot,CurrentTime);
for(i=0;i<hello.size();i++){
    tamper[0] = hello[i];
    KeySym key = XStringToKeysym(tamper);
    XTestFakeKeyEvent(disp,XKeysymToKeycode(disp, key),True, CurrentTime );
    XTestFakeKeyEvent(disp,XKeysymToKeycode(disp, key),False, CurrentTime );
}

Where Select list[selectWindow] is the window where I send data, and tamper a char[2] (to convert char from hello[i] to char * for the function. This code write the content of the hello wariable into the selected window, but the, I tried to send the Return key,

XSetInputFocus(disp, list[selectWindow],RevertToPointerRoot,CurrentTime);
XTestFakeKeyEvent(disp,XKeysymToKeycode(disp, XK_Return),True, CurrentTime );
XTestFakeKeyEvent(disp,XKeysymToKeycode(disp, XK_Return),False, CurrentTime );

So I select the window again, and send the XK_Return key to the application, but it don't work, I think this is because of a 'lag' with Xlib, because if I put a wait(2) at the end of the 'for' loop,it works fine, but I don't want to wait during 2seconds each time I send a message.

I don't know how I can do to solve that.

Thank you.

ex0ns
  • 1,116
  • 8
  • 19
  • I have this exact same problem. I've found two workarounds. First, the waiting after sending. I've found that I can wait much less than 2 seconds. Using usleep it works with only a tenth of a second wait. Second, if I send two (or more) Returns, all but the first work correctly (with no "sleep"ing). – MPT Mar 30 '12 at 15:22
  • @MPT, you can even use `nanosleep()` – user.dz Jan 23 '15 at 13:29

1 Answers1

1

XFlush(disp) or XSync(disp, false) after calling XTestFakeKeyEvent?

pzanoni
  • 2,979
  • 2
  • 18
  • 18