Recent

Author Topic: How to register a windows wise hot key for bringing tray application back?  (Read 543 times)

LeoBruno

  • Jr. Member
  • **
  • Posts: 64
Hi:

I need to register an Operating System wide hot key to bring a hiddn application (minimized to tray) back in to focus.

I have been searching for a solution and found several that does't work animore.

The solution provided here does not work, and the link pointing to another solution is broken.
https://forum.lazarus.freepascal.org/index.php?topic=16733.0

Also, the method that should be called when the hotkey is detected, is declared at the main form class, because it messes with some of that class properties.

Any help?

Thanx...
Lazarus 4.4 FPC 3.2.2 Windows - MetaDarkStyle

Thausand

  • Sr. Member
  • ****
  • Posts: 457
Re: How to register a windows wise hot key for bringing tray application back?
« Reply #1 on: December 06, 2025, 05:55:29 pm »
... and the link pointing to another solution is broken.
Forum have change location then have copy-paste (topic nr same): https://forum.lazarus.freepascal.org/index.php/topic,4964.0.html

Thaddy

  • Hero Member
  • *****
  • Posts: 18695
  • To Europe: simply sell USA bonds: dollar collapses
Re: How to register a windows wise hot key for bringing tray application back?
« Reply #2 on: December 06, 2025, 06:11:26 pm »
Global hotkeys are a can of worms, but the older examples for windows ALL still work, provided you are testing, and this is important, OUTSIDE the Lazarus IDE since that already registers a lot of hotkeys.
Global hotkey? Test OUTSIDE of the IDE and the debugger.

One further caveat is that you can not easily change OS provided hotkeys like alt-tab, alt-f4 and all winkey + key.

You must also ensure the hotkeys are UNregistered when your program terminates, otherwise they will be in effect until system restart and can/WILL cause crashes.

Summary:
Your hotkey will likely work, but you run your program under control of the IDE or the debugger.
And that will fail..... Hotkeys on Windows rely internally on global atoms, independent of fpc/lazarus.

« Last Edit: December 06, 2025, 06:25:45 pm 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...

LeoBruno

  • Jr. Member
  • **
  • Posts: 64
Re: How to register a windows wise hot key for bringing tray application back?
« Reply #3 on: December 06, 2025, 07:43:09 pm »
no they don't.
The code does not compile, and I couldn't figure out whats wrong.

Code: Pascal  [Select][+][-]
  1.  
  2. uses
  3.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, windows;
  4.  
  5. var
  6.   PrevWndProc: WNDPROC;
  7. const
  8.   MY_ID=1;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     procedure FormCreate(Sender: TObject);
  16.   private
  17.   public
  18.     procedure test;
  19.   end;
  20.  
  21. var
  22.   Form1: TForm1;
  23.  
  24. implementation
  25.  
  26. function WndCallback(Ahwnd: HWND; uMsg: UINT; wParam: WParam; lParam: LParam):LRESULT; stdcall;
  27. begin
  28.   if (uMsg=WM_HOTKEY) and (WParam=MY_ID) then
  29.     begin
  30.       Form1.test;
  31.     end;
  32.   result := CallWindowProc(PrevWndProc, Ahwnd, uMsg, WParam, LParam);
  33. end;
  34.  
  35. {$R *.lfm}
  36.  
  37. { TForm1 }
  38.  
  39. procedure TForm1.FormCreate(Sender: TObject);
  40. begin
  41.   PrevWndProc := Windows.WNDPROC(SetWindowLong(Self.Handle, GWL_WNDPROC, PtrInt(@WndCallback)));
  42.   RegisterHotKey(Self.Handle, MY_ID, 0, vk_F9);
  43. end;
  44.  
  45. procedure TForm1.test;
  46. begin
  47.   ShowMessage('test');
  48. end;
  49.  
  50.  

```
Compile Project, Target: C:\Users\Leonardo.Bruno\Documents\project1.exe: Exit code 1, Errors: 1, Hints: 1
unit1.pas(47,74) Hint: Conversion between ordinals and pointers is not portable
unit1.pas(47,18) Error: Illegal type conversion: "LongInt" to "<procedure variable type of function(QWord;LongWord;Int64;Int64):Int64;StdCall>"
```



Global hotkeys are a can of worms, but the older examples for windows ALL still work, provided you are testing, and this is important, OUTSIDE the Lazarus IDE since that already registers a lot of hotkeys.
Global hotkey? Test OUTSIDE of the IDE and the debugger.

One further caveat is that you can not easily change OS provided hotkeys like alt-tab, alt-f4 and all winkey + key.

You must also ensure the hotkeys are UNregistered when your program terminates, otherwise they will be in effect until system restart and can/WILL cause crashes.

Summary:
Your hotkey will likely work, but you run your program under control of the IDE or the debugger.
And that will fail..... Hotkeys on Windows rely internally on global atoms, independent of fpc/lazarus.
Lazarus 4.4 FPC 3.2.2 Windows - MetaDarkStyle

rca

  • Full Member
  • ***
  • Posts: 118
Re: How to register a windows wise hot key for bringing tray application back?
« Reply #4 on: December 06, 2025, 08:42:34 pm »
no they don't.
The code does not compile, and I couldn't figure out whats wrong.

```
Compile Project, Target: C:\Users\Leonardo.Bruno\Documents\project1.exe: Exit code 1, Errors: 1, Hints: 1
unit1.pas(47,74) Hint: Conversion between ordinals and pointers is not portable
unit1.pas(47,18) Error: Illegal type conversion: "LongInt" to "<procedure variable type of function(QWord;LongWord;Int64;Int64):Int64;StdCall>"
```

That error had already been resolved in the forum.

See @440bx's solution here:
https://forum.lazarus.freepascal.org/index.php/topic,67162.msg516256.html#msg516256

Change "SetWindowLong" to "SetWindowLongPtr".

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   PrevWndProc := Windows.WNDPROC(SetWindowLongPtr(Self.Handle,GWL_WNDPROC,PtrInt(@WndCallback)));
  4.   RegisterHotKey(Self.Handle, MY_ID, 0, vk_F9);
  5. end;
  6.  

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1570
    • Lebeau Software
Re: How to register a windows wise hot key for bringing tray application back?
« Reply #5 on: December 06, 2025, 11:59:03 pm »
Change "SetWindowLong" to "SetWindowLongPtr".

Instead of using SetWindowLongPtr(GWL_WNDPROC), a better/safer option is to use SetWindowSubclass().

See Safer subclassing
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Thaddy

  • Hero Member
  • *****
  • Posts: 18695
  • To Europe: simply sell USA bonds: dollar collapses
Re: How to register a windows wise hot key for bringing tray application back?
« Reply #6 on: December 07, 2025, 08:24:15 am »
Same remark as Unregister: don't forget to unsubclass at program termination.
And probably test outside of the IDE/Debugger.
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