Recent

Author Topic: Hotkey problem  (Read 12147 times)

Cz-David

  • New Member
  • *
  • Posts: 12
Hotkey problem
« on: February 05, 2008, 12:47:59 pm »
I'am trying to make windows wide hotkey using register hot key... but declaration in private
Code: [Select]
Procedure WMHotkey( Var msg: TWMHotkey );
    message WM_HOTKEY;

gives this
Code: [Select]
main.pas(41,44) Error: Identifier not found "TWMHotkey"
so i've checked the messages.inc and there is no hotkey what so ever.
then i tried using google... nothing useful for lazarus... in this forum i've found usage of "TMessage" instead of twmhotkey... it does not show any error message but it doesn't work either...
Code: [Select]
procedure Tmain_form.FormCreate(Sender: TObject);
begin
  registerhotkey(handle, 1, mod_control or mod_shift, vk_f2);
end;

Code: [Select]
procedure Tmain_form.wmhotkey(var msg:TMessage); //TWMHotkey
  begin
    showmessage('Byla stisknuta klávesová zkratka');
    msg.Result:=0;
  end;  

Phil

  • Hero Member
  • *****
  • Posts: 2737
Re: Hotkey problem
« Reply #1 on: February 05, 2008, 07:45:57 pm »
Quote from: "Cz-David"
Code: [Select]
main.pas(41,44) Error: Identifier not found "TWMHotkey"


Take a look at this:

http://www.bitwisemag.com/copy/delphi/delphi3.html

TWMHotKey does not appear to be declared in any FPC or LCL unit, but you should be able to use TMessage. The hotkey is passed in the WParam field of the TMesage record.

Thanks.

-Phil

Cz-David

  • New Member
  • *
  • Posts: 12
RE: Re: Hotkey problem
« Reply #2 on: February 05, 2008, 09:59:12 pm »
OK. I've read it... but it doesnt help... I have tried
Code: [Select]
procedure Tmain_form.wmhotkey(var msg:TMessage);
begin
  if msg.wparam = 1 then
  begin
    showmessage('Byla stisknuta klávesová zkratka');
    msg.Result:=0;
  end;
end;

but it still does not work... I think I need more "step by step" help... but thanks anyway

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
RE: Re: Hotkey problem
« Reply #3 on: February 06, 2008, 07:25:13 am »
Maybe LCL has its cross-platform alternative named as TLMHotKey. Remember, in Lazarus all messages are prefixed with LM not WM. Maybe you can find it in LCLType or LCLIntf.

Cz-David

  • New Member
  • *
  • Posts: 12
RE: Re: Hotkey problem
« Reply #4 on: February 06, 2008, 10:03:56 am »
Still same error. I found this
Code: [Select]
 ICC_HOTKEY_CLASS       = $00000040;
in LCLType but this is not very helpful... Are you trying to tell me that no one has ever actually made a working hotkey? :)

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
RE: Re: Hotkey problem
« Reply #5 on: February 11, 2008, 11:02:31 am »
I've checked LMessages as in Delphi TWMHotKey is defined there. It looks like currently LCL doesn't have such a thing. Your code:
Code: [Select]

procedure Tmain_form.FormCreate(Sender: TObject);
begin
  registerhotkey(handle, 1, mod_control or mod_shift, vk_f2);
end;

Seems to work fine. Have you checked the result of registerhotkey?

Cz-David

  • New Member
  • *
  • Posts: 12
RE: Re: Hotkey problem
« Reply #6 on: February 11, 2008, 03:38:18 pm »
Code: [Select]
if not registerhotkey(handle, 1, mod_control or mod_shift, vk_f2) then showmessage('hotkey error');
work fine

Cz-David

  • New Member
  • *
  • Posts: 12
RE: Re: Hotkey problem
« Reply #7 on: February 18, 2008, 11:28:18 am »
User mishob gave me an answer. Working answer. It is kinda bypass for the message function but it works... The virtual hotkey number can be find very easily.
Code: [Select]


type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    procedure Hook;
    procedure Unhook;
  public
    procedure ProcessHotKey(HK: LongInt);
  end;

var
  Form1: TForm1;

type
  TMWndProc = Windows.WNDPROC;

var
  OldProc: TMWndProc;

function MsgProc(Handle: HWnd; Msg: UInt; WParam: Windows.WParam;
                 LParam: Windows.LParam): LResult; stdcall;

implementation


procedure TForm1.FormCreate(Sender: TObject);
begin
  RegisterHotKey(Handle, 0, MOD_WIN, VK_A);
  Hook;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  Unhook;
  UnregisterHotKey(Handle, 0);
end;  

procedure TForm1.Hook;
begin
  OldProc := TMWndProc(Windows.GetWindowLong(Handle, GWL_WNDPROC));
  Windows.SetWindowLong(Handle, GWL_WNDPROC, LongInt(@MsgProc));
end;

procedure TForm1.Unhook;
begin
  if Assigned(OldProc) then
    Windows.SetWindowLong(Handle, GWL_WNDPROC, LongInt(OldProc));
  OldProc := nil;
end;      

procedure TForm1.ProcessHotKey(HK: LongInt);
begin
  // HK is a virtual key code, process it here
end;

function MsgProc(Handle: HWnd; Msg: UInt; WParam: Windows.WParam;
    LParam: Windows.LParam): LResult; stdcall;
begin
  with Form1 do
  begin
    if Msg = WM_HOTKEY then
      ProcessHotKey(HIWORD(LParam))
    else
      Result := Windows.CallWindowProc(OldProc, Handle, Msg, WParam, LParam);
  end;
end;

 

TinyPortal © 2005-2018