Einzelnen Beitrag anzeigen

Viktorii

Registriert seit: 19. Jul 2007
358 Beiträge
 
#1

Hook funtioniert nicht 'global'

  Alt 24. Jul 2013, 10:36
Hallo.

Ich möchte mitbekommen wenn die Strg Taste in irgendeinem Fenster gedrückt wird. Ich habe dazu folgende dll erstellt:

Delphi-Quellcode:
library CtrlWatch;

uses
  Windows,
  Messages,
  System.SysUtils;

var
  HookHandle: Cardinal = 0;
  WindowHandle: Cardinal = 0;
  WM_HOOK: Cardinal;
  gIsPressed: Boolean = false;

function KeyboardHookProc(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
begin
  case nCode < 0 of
    TRUE: exit;
    FALSE:
      begin
        if WParam = VK_CONTROL then
        begin
          if not gIsPressed and (LParam and $80000000 = 0) then
          begin
            gIsPressed := true;
            OutputDebugString('gIsPressed := true');
          end;

          if gIsPressed and (LParam and $80000000 <> 0) then
          begin
            gIsPressed := false;
            OutputDebugString('gIsPressed := false');
          end;
        end;
      end;
  end;

  Result := CallNextHookEx(HookHandle, nCode, wParam, lParam);
end;

function InstallHook(Hwnd: Cardinal): Boolean; stdcall;
begin
  Result := False;
  if HookHandle = 0 then begin
    WM_HOOK := RegisterWindowMessage('WM_HOOK');
    HookHandle := SetWindowsHookEx(WH_KEYBOARD, @KeyboardHookProc, HInstance, 0);
    WindowHandle := Hwnd;
    Result := TRUE;
  end;
end;

function UninstallHook: Boolean; stdcall;
begin
  Result := UnhookWindowsHookEx(HookHandle);
  HookHandle := 0;
end;

exports
  InstallHook,
  UninstallHook;
end.
Wenn ich diese im Debugger laufen lasse, sehe ich dass die Ausgabe funktioniert, aber nur wenn das Fenster der Hostanwendung den Fokus hat.

Gebe ich einem anderen Fenster den Fokus und drücke Strg dann passiert nichts.

Wieso nicht?
  Mit Zitat antworten Zitat