2010-07-01 159 views
6

我创建了一个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; 
+2

您的代码示例有助于很多人试图回答您的代码。 – 2010-07-01 10:37:47

+0

是的,我知道,问题是我不得不提出整个项目,因为我不知道问题出在哪里。我可以说的是,我使用以下设置钩子: hKeyHook:= SetWindowsHookEx(WH_KEYBOARD,KeyHookFunc,hInstance,0); 但我见过一个似乎使用WH_KEYBOARD_LL的C#项目,这可能会有所作为吗? – Paul 2010-07-01 12:04:50

+0

至少您可以向我们展示您在互联网上找到的来源。也许这个代码是不正确的...... – bepe4711 2010-07-01 14:50:04

回答

8

你是不是你的分配和hMemohApp足够早。您正在等待,直到通知的“先前状态”标志为1,表示按键已按住至少1次重复计数,或正在释放,以先发生者为准。因此,hMemohApp在您的挂钩检测到第一次按下键通知时尚不可用。这就是你错过角色的原因。试试这个:

function KeyHookFunc(Code, VirtualKey, KeyStroke: Integer): LRESULT; stdcall; 
var 
    KeyState1: TKeyBoardState; 
    AryChar: array[0..1] of Char; 
    Count: Integer; 
begin 
    Result := CallNextHookEx(hKeyHook, Code, VirtualKey, KeyStroke); 
    if Code <> HC_ACTION then Exit; 

    { a key notification had occured, prepare the HWNDs 
    before checking the actual key state } 
    if (hMemo = 0) or (hApp = 0) then 
    begin 
    if hMemFile = 0 then 
    begin 
     hMemFile := OpenFileMapping(FILE_MAP_WRITE, False, 'NetParentMAP'); 
     if hMemFile = 0 then Exit; 
    end; 
    if PHookRec1 = nil then 
    begin 
     PHookRec1 := MapViewOfFile(hMemFile, FILE_MAP_WRITE, 0, 0, 0); 
     if PHookRec1 = nil then Exit; 
    end; 
    hMemo := PHookRec1.MemoHnd; 
    hApp := PHookRec1.AppHnd; 
    if (hMemo = 0) and (hApp = 0) then Exit; 
    end; 

    if ((KeyStroke and (1 shl 31)) = 0) then // a key is down 
    begin 
    GetKeyboardState(KeyState1); 
    Count := ToAscii(VirtualKey, KeyStroke, KeyState1, AryChar, 0); 
    if Count = 1 then 
    begin 
     if hMemo <> 0 then SendMessage(hMemo, WM_CHAR, Ord(AryChar[0]), 0); 
     if hApp <> 0 then PostMessage(hApp, WM_USER + 1678, Ord(AryChar[0]), 0); 
    end; 
    end; 
end; 
+3

哥们你是明星。可能是愚蠢的错误,但没有更多。谢谢 – Paul 2010-07-02 08:00:58