Recent

Author Topic: How to use RegisterHotKey?  (Read 8475 times)

Marion

  • Jr. Member
  • **
  • Posts: 82
How to use RegisterHotKey?
« on: April 23, 2012, 10:42:51 pm »
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.
Thank you,
Marion
(A recovering Windows programmer.)

Marion

  • Jr. Member
  • **
  • Posts: 82
Re: How to use RegisterHotKey?
« Reply #1 on: April 24, 2012, 12:33:54 pm »
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.
Thank you,
Marion
(A recovering Windows programmer.)

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: How to use RegisterHotKey?
« Reply #2 on: April 24, 2012, 12:46:50 pm »
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
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

avra

  • Hero Member
  • *****
  • Posts: 1717
    • Additional info
Re: How to use RegisterHotKey?
« Reply #3 on: April 24, 2012, 01:26:29 pm »
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
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: How to use RegisterHotKey?
« Reply #4 on: April 24, 2012, 01:47:33 pm »
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,PtrInt(@WndCallback)));
  RegisterHotKey(Self.Handle,MY_ID,0,vk_F9);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  UnRegisterHotkey(Self.Handle,MY_ID);
end;


Marion

  • Jr. Member
  • **
  • Posts: 82
Re: How to use RegisterHotKey?
« Reply #5 on: April 24, 2012, 02:40:02 pm »
Thank you,
Marion
(A recovering Windows programmer.)

Marion

  • Jr. Member
  • **
  • Posts: 82
Re: How to use RegisterHotKey?
« Reply #6 on: April 24, 2012, 02:42:07 pm »
This example does look more straight forward. Thank you.

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,PtrInt(@WndCallback)));
  RegisterHotKey(Self.Handle,MY_ID,0,vk_F9);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  UnRegisterHotkey(Self.Handle,MY_ID);
end;

Thank you,
Marion
(A recovering Windows programmer.)