Recent

Author Topic: Global hotkey to show form - How?  (Read 5824 times)

Jishaxe

  • Full Member
  • ***
  • Posts: 103
  • Hobbist Programmer
Global hotkey to show form - How?
« on: March 25, 2012, 12:04:04 am »
Hey,
I did try Googling but with not many understandable results.
The mainform for my application is hidden, and I want to be able to show it at any time using a hotkey, like Alt + Shift + F3 or something.
How would I do this? It doesn't have to be crossplatform, the app is Windows-only.
A chunk of code and an explanation would be wonderful,
Thank you.
Linux Mint 12
Windows 7 Home Premium
______________________
Definition of programmer: An organism that converts caffeine into software.


RAW

  • Hero Member
  • *****
  • Posts: 868
Re: Global hotkey to show form - How?
« Reply #2 on: August 06, 2017, 08:06:50 am »
This is the easiest way that I know...

Code: Pascal  [Select][+][-]
  1. UNIT Unit1;
  2.  {$MODE OBJFPC}{$H+}{$J-}
  3.  
  4. Interface
  5.  USES
  6.   Windows, Classes, SysUtils, Forms, Controls, Dialogs;
  7.  
  8.  TYPE
  9.   TWMHotKey = Packed Record
  10.    MSG   : Cardinal;
  11.    HotKey: PtrInt;
  12.    Unused: PtrInt;
  13.    Result: PtrInt;
  14.   End;
  15.  
  16.  TYPE
  17.   TForm1 = Class(TForm)
  18.  
  19.    Procedure FormCreate (Sender: TObject);
  20.    Procedure FormClose  (Sender: TObject; Var CloseAction: TCloseAction);
  21.  
  22.     PROTECTED
  23.      Procedure WMHotKey(Var MSG: TWMHotKey); Message WM_HOTKEY;
  24.   End;
  25.  
  26.  VAR
  27.   Form1: TForm1;
  28.  
  29. Implementation
  30.  {$R *.LFM}
  31.  
  32.  
  33. Procedure TForm1.FormCreate(Sender: TObject);
  34.  Begin
  35.   If Not RegisterHotKey(Handle, 111000, MOD_WIN, VK_S)
  36.   Then ShowMessage('HotKey failed...');
  37.  
  38.   // more Keys: MOD_CONTROL+MOD_ALT, VK_S
  39.  End;
  40.  
  41.  
  42. Procedure TForm1.WMHotKey(Var MSG: TWMHotKey);
  43.  Begin
  44.   // Show;
  45.  
  46.   ShowMessage('HotKey');
  47.  End;
  48.  
  49.  
  50. Procedure TForm1.FormClose(Sender: TObject; Var CloseAction: TCloseAction);
  51.  Begin
  52.   UnregisterHotKey(Handle, 111000);
  53.  End;
  54.  
  55. END.
Windows 7 Pro (x64 Sp1) & Windows XP Pro (x86 Sp3).

 

TinyPortal © 2005-2018