Wednesday, April 6, 2011

Capturing F10 in Delphi

I'm trying to capture the F10 key in Delphi, but it seems to keep going to activating the menu because it gets converted from the vk_F10 to vk_menu or something.

From stackoverflow
  • Hello Stephen,

    If you're on Windows, here's some code to make a keyboard hook using the Windows API: http://www.delphitricks.com/source-code/windows/install_a_keyboard_hook.html

  • The following OnKeyDown event added to my main form should work. Note you ned to set the key parameter to zero to prevent menu activation:

    procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    begin
       if key = VK_F10 then begin
         Label1.Caption := 'You hit F10';
         key := 0;
       end;
    end;
    
    mghie : Indeed. But to keep F10 from activating the menu the Key parameter should also be set to 0.
    anon : Actually, it doesn't activate the menu for me as it stands.
    mj2008 : it probably does activate the menu, but that will be lost by the showmessage changing focus. Try setting a label caption instead, and see if it does it then (with a menu present of course!)
    anon : You're right - I'll alter my answer.

0 comments:

Post a Comment