Recent

Author Topic: [SOLVED] Simulating keyboard keys in X gets stuck (as if keyup doesn't work)  (Read 481 times)

dvhx

  • New Member
  • *
  • Posts: 15
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:

Code: Pascal  [Select][+][-]
  1. {$linklib Xtst}
  2.  
  3. uses
  4.   Classes, SysUtils, X, XLib, KeySym;
  5.  
  6. function XTestFakeKeyEvent(dpy: PDisplay; keycode: dword; is_press: boolean;
  7.   delay: dword): longint; cdecl; external;
  8.  
  9. procedure KeyboardSimulator;
  10. var
  11.   Display: PDisplay;
  12.  
  13.   procedure Press(aKeySym: TKeySym);
  14.   begin
  15.     XTestFakeKeyEvent(Display, XKeysymToKeycode(Display, aKeySym), True, 0);
  16.     XTestFakeKeyEvent(Display, XKeysymToKeycode(Display, aKeySym), False, 0);
  17.   end;
  18.  
  19. begin
  20.   Display := XOpenDisplay(nil);
  21.   Press(XK_a);
  22.   Press(XK_b);
  23.   Press(XK_c);
  24.   XFlush(Display);
  25.   XCloseDisplay(Display);
  26. end;
  27.  
  28.  

Then I call it in form1:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.foo(aData: PtrInt);
  2. begin
  3.   KeyboardSimulator;
  4. end;
  5.  
  6. procedure TForm1.Button6Click(Sender: TObject);
  7. begin
  8.   sleep(5000); // in 5s click on some input in other app, e.g. text editor
  9.   Application.QueueAsyncCall(@foo, 123);
  10. 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
« Last Edit: January 29, 2023, 09:50:38 pm by dvhx »

bytebites

  • Hero Member
  • *****
  • Posts: 546
Re: Simulating keyboard keys in X gets stuck (as if keyup doesn't work)
« Reply #1 on: January 29, 2023, 06:24:26 pm »
Code: Pascal  [Select][+][-]
  1. function XTestFakeKeyEvent(dpy: PDisplay; keycode: dword; is_press: boolean32;
  2.   delay: dword): longint; cdecl; external;
Change variable is_press type to boolean32

dvhx

  • New Member
  • *
  • Posts: 15
Re: Simulating keyboard keys in X gets stuck (as if keyup doesn't work)
« Reply #2 on: January 29, 2023, 09:50:18 pm »
It works. Somebody should fix the /usr/lib/lazarus/2.2.0/components/mouseandkeyinput/xkeyinput.pas

dsiders

  • Hero Member
  • *****
  • Posts: 821
Re: Simulating keyboard keys in X gets stuck (as if keyup doesn't work)
« Reply #3 on: January 30, 2023, 12:45:23 am »
It works. Somebody should fix the /usr/lib/lazarus/2.2.0/components/mouseandkeyinput/xkeyinput.pas

If you submit a bug report, someone probably will.
Preview Lazarus 2.3.0 documentation at: https://dsiders.gitlab.io/lazdocsnext

 

TinyPortal © 2005-2018