Recent

Author Topic: [Solved] Can I detect key press while the window is inactive  (Read 986 times)

nikel

  • Sr. Member
  • ****
  • Posts: 282
[Solved] Can I detect key press while the window is inactive
« on: March 27, 2026, 01:48:09 pm »
Hello, I need to create a program which can hadle key press while the window is inactive. Earlier I tried AutoHotKey but Windows Defender quarantined my program.

I'm working with clipboard. For example if I press Alt+n the program must change clipboard content and paste the text.

How can I detect key press and prgrammatically press keys?
« Last Edit: March 27, 2026, 07:18:17 pm by nikel »

mercurhyo

  • Sr. Member
  • ****
  • Posts: 265
Re: Can I detect key press while the window is inactive
« Reply #1 on: March 27, 2026, 03:44:02 pm »
Global key hooking is the virus‑syndrome of keyloggers.
LCL, FCL, Tarzan and even myself do not provide that kind of disease.
 O:-)
Jungle Debug ON.
DEO MERCHVRIO - Fedora PlasmaKDE, Win11pro - Ryzen9XT+Geforce Rtx 3080SUPRIM
god of financial gain, commerce, eloquence (and thus poetry), messages, communication (including divination), travelers, boundaries, luck, trickery and thieves; he also serves as the guide of souls to the underworld

nikel

  • Sr. Member
  • ****
  • Posts: 282
Re: Can I detect key press while the window is inactive
« Reply #2 on: March 27, 2026, 03:51:57 pm »
Thanks for the hint. I'll keep in mind that.

mercurhyo

  • Sr. Member
  • ****
  • Posts: 265
Re: Can I detect key press while the window is inactive
« Reply #3 on: March 27, 2026, 03:59:00 pm »
 :)
DEO MERCHVRIO - Fedora PlasmaKDE, Win11pro - Ryzen9XT+Geforce Rtx 3080SUPRIM
god of financial gain, commerce, eloquence (and thus poetry), messages, communication (including divination), travelers, boundaries, luck, trickery and thieves; he also serves as the guide of souls to the underworld

LemonParty

  • Hero Member
  • *****
  • Posts: 537
Re: Can I detect key press while the window is inactive
« Reply #4 on: March 27, 2026, 04:10:49 pm »
Windows only solution. This method require for you to have at least 1 form.
Code: Pascal  [Select][+][-]
  1. { this not work yet with UNIX }
  2.  
  3. unit uHotkey;
  4.  
  5. {$mode objfpc}{$H+}
  6.  
  7. interface
  8.  
  9. uses
  10.  Classes, SysUtils, Forms{$IfDef Windows}, windows{$EndIf}, process;
  11.  
  12. const
  13.   MY_ID = 13;
  14.  
  15. var
  16.   H: HANDLE; {set this before StartHotkey}
  17.   Callback: TProcedure;
  18.   PrevWndProc: WNDPROC;
  19.  
  20. procedure StartHotkey;
  21. procedure StopHotkey;
  22.  
  23. implementation
  24.  
  25. {$IfDef Windows}
  26. function WndCallback(hW: HWND; uMsg: UINT; wP: WParam; lP: LParam): LRESULT; stdcall;
  27. begin
  28.   if (uMsg=WM_HOTKEY) and (wP=MY_ID) then begin
  29.     Callback;
  30.   end;
  31.   Result:= CallWindowProc(PrevWndProc, hW, uMsg, wP, lP);
  32. end;
  33. {$EndIf}
  34.  
  35. procedure StartHotkey;
  36. var k: WINBOOL;
  37. begin
  38. {$IfDef Windows}
  39.   PrevWndProc:= Windows.WNDPROC(SetWindowLongPtr(H, GWL_WNDPROC, PtrInt(@WndCallback)));
  40.   RegisterHotKey(H, MY_ID, MOD_ALT or MOD_SHIFT, VK_M);
  41. {$EndIf}
  42. end;
  43.  
  44. procedure StopHotkey;
  45. begin
  46. {$IfDef Windows}
  47.   UnRegisterHotkey(H, MY_ID);
  48. {$EndIf}
  49. end;
  50.  
  51. end.
  52.  

How to use. In FormCreate you do:
Code: Pascal  [Select][+][-]
  1.   uHotkey.H:= Handle;
  2.   uHotkey.Callback:= @Run;
  3.   uHotkey.StartHotkey;
  4.  
(Run is a simple procedure that you defined myself)
Then in FormDestroy:
Code: Pascal  [Select][+][-]
  1.   uHotkey.StopHotkey;
  2.  
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

nikel

  • Sr. Member
  • ****
  • Posts: 282
Re: Can I detect key press while the window is inactive
« Reply #5 on: March 27, 2026, 07:17:54 pm »
Thanks a lot! You're a great coder.

Modify:
I had to change
Code: Pascal  [Select][+][-]
  1. if (uMsg = WM_HOTKEY) and (wP = MY_ID) then
  2.   Callback;

to:
Code: Pascal  [Select][+][-]
  1. if (uMsg = WM_HOTKEY) and (wP = MY_ID) then
  2.   Callback(nil);
« Last Edit: March 27, 2026, 07:28:50 pm by nikel »

LemonParty

  • Hero Member
  • *****
  • Posts: 537
Re: [Solved] Can I detect key press while the window is inactive
« Reply #6 on: March 27, 2026, 08:45:57 pm »
Also you can use this project if you need Linux too:
https://github.com/TonyStone31/LazHIDControl
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

 

TinyPortal © 2005-2018