delphi - Windows 全局键盘 Hook - Delphi

标签 delphi keyboard-hook setwindowshookex

我使用在互联网上找到的源代码创建了一个全局键盘 Hook DLL。就最好的部分而言,它工作得非常出色,除了浏览器之外。

它会拾取浏览器中的每个键,但看起来,当浏览器获得焦点时,它会丢失按下的第一个键。在 IE 和 Firefox 中进行了测试,两者似乎是相同的。

例如,如果我打开 IE 并开始输入 www. ,我只回来ww。如果浏览器窗口保持焦点,则不会丢失更多键。一旦浏览器失去焦点并重新获得焦点,第一个键就会再次丢失。

可能是因为只使用了 WH_KEYDOWN 而不是 WH_KEYPRESS/WH_KEYUP 吗?有人可以解释一下吗?

谢谢

PS:钩子(Hook)函数本身如下:DLL 被发送一个备忘录框和应用程序句柄,DLL 将向其发送消息以及用户消息。

    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; 

最佳答案

您没有尽早分配 hMemohApp 值。您正在等待,直到“先前状态”标志为 1 的通知,这表示某个键已被按住至少 1 次重复计数,或者正在被释放,以先发生者为准。因此,当您的钩子(Hook)检测到第一个按键通知时,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;

关于delphi - Windows 全局键盘 Hook - Delphi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3156418/

相关文章:

java - C++/Java - 键盘 Hook

c++ - 如何在Windows 7下拦截Windows key ?

hook - Win32 将 DLL 注入(inject)到针对 "Any CPU"构建的应用程序中

c# - 是否可以检测 Windows 何时自动断开低级键盘 Hook ?

arrays - 如何对末尾带有零的整数数组进行排序?

delphi - Delphi和大型数据库的最佳数据库

c# - 是否可以轮询 GetKeyboardState() 作为全局键盘 Hook 的替代品?

c# - SetWindowsHookEx 失败并出现错误 126

delphi - 无法将函数引用从框架传递到表单

Delphi FMX TCheckbox/TRadiobutton 自动调整大小