6

インターネットで見つけたソース コードを使用して、グローバル キーボード フック DLL を作成しました。最良の部分は、ブラウザを除いて、見事に機能します。

ブラウザがフォーカスを取得すると、押された最初のキーが失われるように見えることを除いて、ブラウザのすべてのキーを取得します。これを IE と Firefox でテストしたところ、どちらも同じようです。

たとえば、IE を開いて www と入力すると、、帰るしかないww。ブラウザ ウィンドウがフォーカスされたままであれば、それ以上キーが失われることはありません。ブラウザーがフォーカスを失い、フォーカスを取り戻すとすぐに、最初のキーが再び失われます。

WH_KEYPRESS / WH_KEYUP の代わりに WH_KEYDOWN のみを使用しているためでしょうか? 誰でもこれに光を当てることができますか?

ありがとうございました

PS: フック関数自体は以下のとおりです: 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; 
4

1 に答える 1

8

hMemoと のhApp値を十分早く割り当てていません。「前の状態」フラグが 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;
于 2010-07-02T07:36:40.360 に答える