I've tried to use MouseAndKeyInput unit to simulate typing in other app but it didn't work correctly because the last character gets stuck and keeps repeating until I physically press it and release it on my keyboard. I isolated minimal code but it is doing the same.
Here is code:
{$linklib Xtst}
uses
Classes, SysUtils, X, XLib, KeySym;
function XTestFakeKeyEvent(dpy: PDisplay; keycode: dword; is_press: boolean;
delay: dword): longint; cdecl; external;
procedure KeyboardSimulator;
var
Display: PDisplay;
procedure Press(aKeySym: TKeySym);
begin
XTestFakeKeyEvent(Display, XKeysymToKeycode(Display, aKeySym), True, 0);
XTestFakeKeyEvent(Display, XKeysymToKeycode(Display, aKeySym), False, 0);
end;
begin
Display := XOpenDisplay(nil);
Press(XK_a);
Press(XK_b);
Press(XK_c);
XFlush(Display);
XCloseDisplay(Display);
end;
Then I call it in form1:
procedure TForm1.foo(aData: PtrInt);
begin
KeyboardSimulator;
end;
procedure TForm1.Button6Click(Sender: TObject);
begin
sleep(5000); // in 5s click on some input in other app, e.g. text editor
Application.QueueAsyncCall(@foo, 123);
end;
In this case it type "abc" and then it get's stuck typing "c" forever until I press "c" to release it.
2.2.0+dfsg1-5ubuntu1, FPC version 3.2.2, Revision Debian package 2.2.0+dfsg1-5ubuntu1, x6_64-linux-gtk2