How to use RegisterHotKey? lazarus / 34-Windows ---------------------------------------------------------------------- Marion · 2012-04-23 20:42 Can someone explain or provide an example of using RegisterHotKey? I just need to detect if the user hits F9 and I will unhide my window. ---------------------------------------------------------------------- Marion · 2012-04-24 10:33 I am afraid this is going to be the hardest requirement for Lazarus to fulfill. I have been looking through this forum and it seems this is a challenge. If it makes it easier, it only has to work on Windows. Please, this is the last requirement that Lazarus has to meet. ---------------------------------------------------------------------- BigChimp · 2012-04-24 10:46 Marion, Assuming there's no documentation in the wiki either: you can also ask on the Lazarus mailing list... perhaps you'll have more luck with a quick answer there.. Good luck, BigChimp ---------------------------------------------------------------------- avra · 2012-04-24 11:26 In my Lazarus CodeTyphon installation I have RegisterHotKey API call defined in this converted JEDI file: c:\codetyphon\fpcsrc\packages\winunits-jedi\src\jwawinuser.pas and it looks something like this: Code: [Select]... function RegisterHotKey(hWnd: HWND; id: Integer; fsModifiers, vk: UINT): BOOL; stdcall; {$EXTERNALSYM RegisterHotKey} function UnregisterHotKey(hWnd: HWND; id: Integer): BOOL; stdcall; {$EXTERNALSYM UnregisterHotKey} ... var   _RegisterHotKey: Pointer; function RegisterHotKey; begin   GetProcedureAddress(_RegisterHotKey, user32, 'RegisterHotKey');   asm         MOV     ESP, EBP         POP     EBP         JMP     [_RegisterHotKey]   end; end; var   _UnregisterHotKey: Pointer; function UnregisterHotKey; begin   GetProcedureAddress(_UnregisterHotKey, user32, 'UnregisterHotKey');   asm         MOV     ESP, EBP         POP     EBP         JMP     [_UnregisterHotKey]   end; end; ... function RegisterHotKey; external user32 name 'RegisterHotKey'; function UnregisterHotKey; external user32 name 'UnregisterHotKey'; I guess it shouldn't be hard to use that API call like shown here: http://www.delphigroups.info/2/77/488140.html ---------------------------------------------------------------------- ludob · 2012-04-24 11:47 The difficulty is that, contrary to Delphi, Lazarus doesn't allow to easily handle no user messages such as WM_HOTKEY. The following code does what you want (system wide F9 restores app): Code: [Select]uses ...,windows; var   PrevWndProc: WNDPROC; const   MY_ID=1; function WndCallback(Ahwnd: HWND; uMsg: UINT; wParam: WParam; lParam: LParam):LRESULT; stdcall; begin   if (uMsg=WM_HOTKEY) and (WParam=MY_ID) then     begin       Application.Restore;     end;   result:=CallWindowProc(PrevWndProc,Ahwnd, uMsg, WParam, LParam); end; procedure TForm1.FormCreate(Sender: TObject); begin   PrevWndProc:=Windows.WNDPROC(SetWindowLong(Self.Handle,GWL_WNDPROC,P trInt(@WndCallback)));   RegisterHotKey(Self.Handle,MY_ID,0,vk_F9); end; procedure TForm1.FormDestroy(Sender: TObject); begin   UnRegisterHotkey(Self.Handle,MY_ID); end; ---------------------------------------------------------------------- Marion · 2012-04-24 12:40 I also found a working example here: http://www.lazarus.freepascal.org/index.php/topic,4964.msg23924.html#m sg23924 ---------------------------------------------------------------------- Marion · 2012-04-24 12:42 This example does look more straight forward. Thank you. Quote from: ludob on April 24, 2012, 01:47:33 pmThe difficulty is that, contrary to Delphi, Lazarus doesn't allow to easily handle no user messages such as WM_HOTKEY. The following code does what you want (system wide F9 restores app): Code: [Select]uses ...,windows; var   PrevWndProc: WNDPROC; const   MY_ID=1; function WndCallback(Ahwnd: HWND; uMsg: UINT; wParam: WParam; lParam: LParam):LRESULT; stdcall; begin   if (uMsg=WM_HOTKEY) and (WParam=MY_ID) then     begin       Application.Restore;     end;   result:=CallWindowProc(PrevWndProc,Ahwnd, uMsg, WParam, LParam); end; procedure TForm1.FormCreate(Sender: TObject); begin   PrevWndProc:=Windows.WNDPROC(SetWindowLong(Self.Handle,GWL_WNDPROC,P trInt(@WndCallback)));   RegisterHotKey(Self.Handle,MY_ID,0,vk_F9); end; procedure TForm1.FormDestroy(Sender: TObject); begin   UnRegisterHotkey(Self.Handle,MY_ID); end;