如何在应用程序中的F1上应用键盘钩 [英] How to apply a keyboardhook on F1 within the application
问题描述
对于我们的应用程序,我们创建了一个在线帮助,客户可以在其中查找有关我们应用程序的信息.他们可以在菜单中找到它,但我也想通过在我们应用程序中的任何位置按F1键来使它可用(因为该键主要用于其他应用程序中的帮助).
For our application we created a Online Help where customers can look up stuff about our application. They can reach this in the menus but I also wanted to make it available by pressing the F1 key where ever you are within our application(since this is mostly used for Help within other applications).
我尝试使用RegisterHotKey函数,但事实证明,这会在整个系统范围内注册热键.当您在我们的应用程序中时,我只希望它打开我们的在线帮助.
I tried using the RegisterHotKey function but as it turns out, this registers the hotkey system wide. I only want it to open our Online Help when you are within our application.
因此,我尝试设置键盘挂钩,但这似乎也适用于整个系统.有没有一种方法可以确保在应用程序中按F1键将其打开,但是当您没有将应用程序聚焦时,该方法就无法打开吗?
So i tried to setup a Keyboard hook but this also seems to apply it system wide. Is there a way to make sure that pressing the F1 key within the application will open it, but not when you don't have the application focused?
编码我尝试过的内容:
procedure WMHotKey(var Msg: TWMHotKey); message WM_HOTKEY;
procedure TZZ_Main_Form.WMHotKey(var Msg: TWMHotKey);
begin
if Msg.HotKey = HotKeyIDF1 then btOnlineHelpClick(nil);
end;
在FormCreate和FormDestroy中:
Within the FormCreate and FormDestroy:
RegisterHotKey(Handle, HotKeyIDF1, 0, VK_F1);
UnRegisterHotKey(Handle, HotKeyIDF1);
关于键盘挂钩,我尝试过的事情:
Regarding the keyboard hook, what i tried:
hkHook := SetWindowsHookEx(WH_KEYBOARD,@KeyboardProc,hInstance,GetCurrentThreadID());
function KeyboardProc(Code, wParam, lParam: Integer): Integer;
var
url: String;
ShellInfo: TShellExecuteInfo;
begin
try
case wParam of
VK_F1:
begin
url := 'ourOnlineHelpLink'
if url <> '' then
begin
FillChar( ShellInfo, SizeOf( TShellExecuteInfo ), 0 );
ShellInfo.cbSize := SizeOf( TShellExecuteInfo );
ShellInfo.fMask := SEE_MASK_NOCLOSEPROCESS or SEE_MASK_FLAG_NO_UI or
SEE_MASK_FLAG_DDEWAIT;
ShellInfo.Wnd := HWnd_Desktop;
ShellInfo.lpVerb := 'OPEN';
ShellInfo.lpFile := PChar( url );
ShellInfo.lpParameters := nil;
ShellInfo.lpDirectory := nil;
ShellInfo.nShow := sw_ShowNormal;
ShellExecuteEx( @ShellInfo );
end;
end;
end;
finally
Result := -1;
end;
end;
推荐答案
您可以使用 TApplicationEvents.OnHelp
在应用程序范围内拦截 F1 .
You can intercept the F1 application-wide by using TApplicationEvents.OnHelp
.
将组件拖放到您的主窗体上,或在主窗体之前创建的数据模块中.双击事件选项卡上的 OnHelp
事件,并添加类似于以下内容的代码(在Delphi 2007之后,参数类型发生了变化,因此我对这两种方法都提供了支持):
Drop the component on your main form, or in a datamodule that is created before your main form. Double-click the OnHelp
event on its events tab, and add code similar to the following (the parameter types changed after Delphi 2007, so I've included support for both):
// Delphi 2007 and earlier
{$IFDEF VER185}
function TMainForm.ApplicationEvents1Help(Command: Word; Data: Integer;
var CallHelp: Boolean): Boolean;
begin
CallHelp := False;
// Call your own procedure to implement help as you'd like
Result := True;
end;
{$ELSE Greater than D2007}
function TMainForm.ApplicationEvents1Help(Command: Word; Data: NativeInt;
var CallHelp: Boolean): Boolean;
begin
CallHelp := False;
// Call your own procedure to implement help as you'd like
Result := True;
end;
{$ENDIF}
这篇关于如何在应用程序中的F1上应用键盘钩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!