Recent

Author Topic: keybd_event  (Read 1740 times)

Ed78z

  • Jr. Member
  • **
  • Posts: 55
keybd_event
« on: July 04, 2025, 01:31:41 am »
Hello,
I can't find a solution to simulate keyboard in case of more than two keys combination.

For example, simulating of WIN+SHIFT+UP_arrow
Code: Pascal  [Select][+][-]
  1. Uses Windows;
  2.  
  3. Procedure MaximizeWindowVertically;
  4. begin
  5.   keybd_event(VK_LWIN, 0, 0, 0); // Left Windows key down
  6.   keybd_event(VK_SHIFT, 0, 0, 0); // Shift key down
  7.   keybd_event(VK_UP, 0, 0, 0); // Up arrow key down
  8.  
  9.   keybd_event(VK_UP, 0, KEYEVENTF_KEYUP, 0); // Up arrow key up
  10.   keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0); // Shift key up
  11.   keybd_event(VK_LWIN, 0, KEYEVENTF_KEYUP, 0); // Left Windows key up
  12. end;
  13.  

This code doesn't work! it just simulating WIN+UP_arrow (two keys)

Do you have any idea how to simulate three keys combination?

jamie

  • Hero Member
  • *****
  • Posts: 7770
Re: keybd_event
« Reply #1 on: July 04, 2025, 01:51:32 am »
The only true wisdom is knowing you know nothing

Fibonacci

  • Hero Member
  • *****
  • Posts: 1000
  • Behold, I bring salvation - FPC Unleashed
Re: keybd_event
« Reply #2 on: July 04, 2025, 02:11:18 am »
Use this instead.

https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-sendinput

 jamie

Same side effect: the start menu pops up as if WIN+SHIFT was pressed, but it also works and maximizes the window vertically.

But MoveWindow() can do the job. GPT-generated code.

Code: Pascal  [Select][+][-]
  1. uses JwaWindows{for MONITORINFO}, Windows;
  2.  
  3. procedure MaximizeVertically;
  4. var
  5.   mi: MONITORINFO;
  6.   rc: TRect;
  7.   h: HWND;
  8. begin
  9.   h := GetForegroundWindow;
  10.   mi.cbSize := SizeOf(mi);
  11.   GetMonitorInfo(MonitorFromWindow(h, MONITOR_DEFAULTTONEAREST), @mi);
  12.   GetWindowRect(h, rc);
  13.   MoveWindow(h,
  14.     rc.Left,
  15.     mi.rcWork.Top,
  16.     rc.Right - rc.Left,
  17.     mi.rcWork.Bottom - mi.rcWork.Top,
  18.     True);
  19. end;
  20.  
  21. begin
  22.   writeln('testing in 1s...');
  23.   sleep(1000);
  24.  
  25.   MaximizeVertically;
  26.  
  27.   writeln('worked?');
  28.   readln;
  29. end.
FPC Unleashed - inline vars, tuples, statement expressions, array equality, compound assignments, indexed/lazy labels, no-RTTI & more. ⭐ Star it on GitHub!

Thaddy

  • Hero Member
  • *****
  • Posts: 19268
  • Glad to be alive.
Re: keybd_event
« Reply #3 on: July 04, 2025, 05:13:04 pm »
What is wrong with the shortcut editor? (nothing)
Define an action first and call that.
That's also cross platform instead of the windows keybd_event stuff.
« Last Edit: July 04, 2025, 06:21:09 pm by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

n7800

  • Hero Member
  • *****
  • Posts: 710
  • Lazarus IDE contributor
    • GitLab profile
Re: keybd_event
« Reply #4 on: July 05, 2025, 02:46:13 pm »
This code doesn't work! it just simulating WIN+UP_arrow (two keys)

Do you have any idea how to simulate three keys combination?

Add the KEYEVENTF_EXTENDEDKEY flag. Unfortunately, the description of this flag is unclear, and I don't know when it is needed. But at least it helped.
« Last Edit: July 05, 2025, 02:48:09 pm by n7800 »

n7800

  • Hero Member
  • *****
  • Posts: 710
  • Lazarus IDE contributor
    • GitLab profile
Re: keybd_event
« Reply #5 on: July 05, 2025, 02:52:52 pm »
Of course, some information can be found on the Internet. For example, here keybd_event is even mentioned.

Thaddy

  • Hero Member
  • *****
  • Posts: 19268
  • Glad to be alive.
Re: keybd_event
« Reply #6 on: July 05, 2025, 03:59:25 pm »
Of course, some information can be found on the Internet. For example, here keybd_event is even mentioned.
In the context of winapi, duh... Lazarus only support that on windows because it is windows specific.
objects are fine constructs. You can even initialize them with constructors.

 

TinyPortal © 2005-2018