我创建了一个GLOBAL键盘钩子DLL,使用在互联网上找到的源代码。对于最好的部分来说,除了涉及到浏览器以外,它的功能非常出色。Windows全局键盘钩子 - Delphi
它接收浏览器中的每个按键,但看起来,当浏览器获得焦点时,它会丢失按下的第一个按键。在IE和Firefox中测试过它,两者似乎都是一样的。
例如,如果我打开IE并开始输入www。 ,我只能回来ww。如果浏览器窗口始终处于焦点状态,则不会再有密钥丢失。一旦浏览器失去焦点并重新获得焦点,第一个关键就会再次失踪。
难道是因为只使用WH_KEYDOWN而不是WH_KEYPRESS/WH_KEYUP?任何人都可以对此有所了解吗?
谢谢
PS:钩子函数本身就是下图:DLL发送了一份备忘录箱和应用程序句柄至极DLL将发送消息,以及一个usermessage。
function KeyHookFunc(Code, VirtualKey, KeyStroke: Integer): LRESULT; stdcall;
var
KeyState1: TKeyBoardState;
AryChar: array[0..1] of Char;
Count: Integer;
begin
Result := 0;
if Code = HC_NOREMOVE then Exit;
Result := CallNextHookEx(hKeyHook, Code, VirtualKey, KeyStroke);
{I moved the CallNextHookEx up here but if you want to block
or change any keys then move it back down}
if Code < 0 then
Exit;
if Code = HC_ACTION then
begin
if ((KeyStroke and (1 shl 30)) <> 0) then
if not IsWindow(hMemo) then
begin
{I moved the OpenFileMapping up here so it would not be opened
unless the app the DLL is attatched to gets some Key messages}
hMemFile := OpenFileMapping(FILE_MAP_WRITE, False, 'NetParentMAP');//Global7v9k
PHookRec1 := MapViewOfFile(hMemFile, FILE_MAP_WRITE, 0, 0, 0);
if PHookRec1 <> nil then
begin
hMemo := PHookRec1.MemoHnd;
hApp := PHookRec1.AppHnd;
end;
end;
if ((KeyStroke AND (1 shl 31)) = 0) then //if ((KeyStroke and (1 shl 30)) <> 0) then
begin
GetKeyboardState(KeyState1);
Count := ToAscii(VirtualKey, KeyStroke, KeyState1, AryChar, 0);
if Count = 1 then
begin
SendMessage(hMemo, WM_CHAR, Ord(AryChar[0]), 0);
{I included 2 ways to get the Charaters, a Memo Hnadle and
a WM_USER+1678 message to the program}
PostMessage(hApp, WM_USER + 1678, Ord(AryChar[0]), 0);
end;
end;
end;
end;
您的代码示例有助于很多人试图回答您的代码。 – 2010-07-01 10:37:47
是的,我知道,问题是我不得不提出整个项目,因为我不知道问题出在哪里。我可以说的是,我使用以下设置钩子: hKeyHook:= SetWindowsHookEx(WH_KEYBOARD,KeyHookFunc,hInstance,0); 但我见过一个似乎使用WH_KEYBOARD_LL的C#项目,这可能会有所作为吗? – Paul 2010-07-01 12:04:50
至少您可以向我们展示您在互联网上找到的来源。也许这个代码是不正确的...... – bepe4711 2010-07-01 14:50:04