Recent

Author Topic: Simulating Keypress  (Read 1319 times)

Aruna

  • Hero Member
  • *****
  • Posts: 794
Simulating Keypress
« on: September 03, 2024, 05:42:57 pm »
What is the best way to simulate a keypress programatically? Second question: What if I need a combination of key presses, like Ctrl + Alt + <some key> ?


Handoko

  • Hero Member
  • *****
  • Posts: 5524
  • My goal: build my own game engine using Lazarus
Re: Simulating Keypress
« Reply #1 on: September 03, 2024, 06:12:23 pm »
I am not sure, I haven't tried it. But maybe this is what you're looking for:

https://wiki.lazarus.freepascal.org/MouseAndKeyInput

Aruna

  • Hero Member
  • *****
  • Posts: 794
Re: Simulating Keypress
« Reply #2 on: September 03, 2024, 06:33:41 pm »
I am not sure, I haven't tried it. But maybe this is what you're looking for:

https://wiki.lazarus.freepascal.org/MouseAndKeyInput
Thank you very much @Handoko I did go through that before I asked here. It is a rather nice demo where the mouse movement is controlled using code and it has this below for sending keys:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.SendKeys(Data: PtrInt);
  2. begin
  3.   Edit1.SetFocus;
  4.   Application.ProcessMessages;
  5.   KeyInput.Press(VK_H);
  6.   KeyInput.Press(VK_E);
  7.   KeyInput.Press(VK_L);
  8.   KeyInput.Press(VK_L);
  9.   KeyInput.Press(VK_O);
  10. end;                  
The problem is no where does it show how to send more than a single keypress at the same time. That example prints out 'HELLO' in the Edit1 which is fine but what if I want to send a combination of keys like Shift+Alt+Ctrl+<more keys> ? This is what I am trying to find out.

TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: Simulating Keypress
« Reply #3 on: September 03, 2024, 07:25:17 pm »
There are up and down methods ?

Even if there is a notion that all keys are pressed at the same time, it actually isn't. At least not from nature. Most keyboard do not even support pressing more than an x amount of keys at the same time  :)

edit: forgot to mention, there is also a method named apply where you can apply the shiftstate for 'those special keys'
« Last Edit: September 03, 2024, 07:51:55 pm by TRon »
Today is tomorrow's yesterday.

Aruna

  • Hero Member
  • *****
  • Posts: 794
Re: Simulating Keypress
« Reply #4 on: September 03, 2024, 08:00:43 pm »
There are up and down methods ?
Hi @TRon, Yes we have up and down methods but I have questions:
 
Code: Pascal  [Select][+][-]
  1. type
  2.   { TKeyInput }
  3.  
  4.   TKeyInput = class
  5.   protected
  6.     procedure DoDown(Key: Word); dynamic; abstract;
  7.     procedure DoUp(Key: Word); dynamic; abstract;
  8.   public
  9.     procedure Down(Key: Word);
  10.     procedure Up(Key: Word);
  11.    
  12.     procedure Press(Key: Word);
  13.     procedure Press(StringValue : String);
  14.    
  15.     procedure Apply(Shift: TShiftState);
  16.     procedure Unapply(Shift: TShiftState);
  17.   end;                                
How can we have the exact same procedure name? Please explain this to me because I am very confused because it compiles without complaining. See below:

    procedure Press(Key: Word);
    procedure Press(StringValue : String); 

The other thing that baffles me is for me to do what I want I have to press each key and HOLD then press the next key. So press CTRL and hold, press Shift and hold then press Alt and hold so what ever happnes to the keyboard buffer ( overflows a zillion times? but does not freeze? )
Even if there is a notion that all keys are pressed at the same time, it actually isn't. At least not from nature. Most keyboard do not even support pressing more than an x amount of keys at the same time  :)
Just so you know the reason am searching for this is because @dbannon posted recently about moving a window from one workspace to another in Linux and I started digging and on my Debian and Gnome system when I press Ctrl+Alt+Shift+<left or right arrow key> I can switch to another workspace. So I was trying to simulate this in code and send @dbanon's way but things are not as simple as they look. I also found you can set this to switch workspaces: _NET_WM_DESKTOP

I know people will hang me high one of these days but I asked chatGPT and I got back this:

Steps to Set _NET_WM_DESKTOP

1    Open a Connection to the X Server: Use XOpenDisplay to establish a connection with the X server.
2    Retrieve the Atom for _NET_WM_DESKTOP: Use XInternAtom to get the atom corresponding to the _NET_WM_DESKTOP property.
3    Prepare the Data: Prepare the data you want to set. This will be the workspace number you want to assign to the window.
4    Set the Property Using XChangeProperty: Use XChangeProperty to set the property on the window.
5    Flush the Display: Ensure that the changes are sent to the X server by using XFlush.
6    Close the Connection: Close the connection to the X server using XCloseDisplay.

Seems easy enough right? NOt so am afraid :-) If you have any code please do share!

TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: Simulating Keypress
« Reply #5 on: September 03, 2024, 08:12:16 pm »
How can we have the exact same procedure name? Please explain this to me because I am very confused because it compiles without complaining. See below:
For an explanation, see overload and function overloading.

Basically you can overload procedures/function withthe same name (as long as the parametertypes differ).

Quote
The other thing that baffles me is for me to do what I want I have to press each key and HOLD then press the next key. So press CTRL and hold, press Shift and hold then press Alt and hold so what ever happnes to the keyboard buffer ( overflows a zillion times? but does not freeze? )
Not exactly. The keyboard buffer registers the up/down events in a specific order. a Keyboard-driver and/or OS then tries to make something useful out of that.


Quote
I know people will hang me high one of these days but I asked chatGPT and I got back this:
In this case I am not that surprised to resort to other sources (I got such a similar answer using SO (Stack Overflow)). afaik It is (currently) missing functionality from the LCL.

« Last Edit: September 03, 2024, 08:15:14 pm by TRon »
Today is tomorrow's yesterday.

Thaddy

  • Hero Member
  • *****
  • Posts: 18783
  • To Europe: simply sell USA bonds: dollar collapses
Re: Simulating Keypress
« Reply #6 on: September 04, 2024, 06:54:06 am »
Written by our member Jeff Duntemann. Very old but very good for Windows.
Used to come with Delphi. FPC version with permission. Not tested with LCL, but good chance it is cross platform with lclintf. This is straight from my backup's so it may be a little bit tainted. (not the code).
Jeff probably has the original. As do I but I have no  floppy drive anymore.
« Last Edit: September 04, 2024, 07:38:57 am by Thaddy »
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

 

TinyPortal © 2005-2018