Recent

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

dvhx

  • New Member
  • *
  • Posts: 17
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: 633
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: 17
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: 1052
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 3.99 documentation at: https://dsiders.gitlab.io/lazdocsnext

desliem

  • New Member
  • *
  • Posts: 12
@dsiders

Submitted MouseAndKeyInput (Linux: XTestFakeKeyEvent) no key up event with certain keys (https://gitlab.com/freepascal.org/lazarus/lazarus/-/issues/39964) at your behest (https://forum.lazarus.freepascal.org/index.php/topic,33719.msg457409.html#msg457409) nine months ago. Unfortunately, the report had no traction.

Perhaps if someone with more credibility submitted a third bug report, the simple solution could be applied to get rid of a bug that has been around for 8 years.

Update: Dušan Halický  raised the issue six months ago in response to this topic (https://gitlab.com/freepascal.org/lazarus/lazarus/-/issues/40102) and Juha Manninen corrected xkeyinput.pas (commit 01215457:  https://gitlab.com/freepascal.org/lazarus/lazarus/-/commit/012154575832231939d58b13aa0e5a373aca531d)  and xmouseandkeyinput.pas (commit 72040bb5: https://gitlab.com/freepascal.org/lazarus/lazarus/-/commit/72040bb55d927ed3c9902c5d5e342bcde38d2d23) which had the same problem.

Lazarus 2.2.7 (rev lazarus_2_2_6-1-gada7a90f86) FPC 3.2.3 x86_64-linux-qt5 dated 2023-08-10 (two weeks ago) does not contain the corrections, but hopefully the commits will soon make their way to the next stable release.
« Last Edit: August 22, 2023, 01:30:05 am by desliem »

 

TinyPortal © 2005-2018