Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Programmieren allgemein (https://www.delphipraxis.net/40-programmieren-allgemein/)
-   -   Hook funtioniert nicht 'global' (https://www.delphipraxis.net/175854-hook-funtioniert-nicht-global.html)

Viktorii 24. Jul 2013 10:36

Hook funtioniert nicht 'global'
 
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?

Der schöne Günther 24. Jul 2013 11:45

AW: Hook funtioniert nicht 'global'
 
KeyboardProc:
Zitat:

The system calls this function whenever an application calls the GetMessage or PeekMessage function and there is a keyboard message (WM_KEYUP or WM_KEYDOWN) to be processed.
LowLevelKeyboardProc:
Zitat:

. The system calls this function every time a new keyboard input event is about to be posted into a thread input queue.
Könnte das daran liegen? Mal mit einem
Delphi-Quellcode:
WH_KEYBOARD_LL
probiert?

Viktorii 25. Jul 2013 07:32

AW: Hook funtioniert nicht 'global'
 
Scheint zu funktionieren.

Vielen Dank.


Alle Zeitangaben in WEZ +1. Es ist jetzt 19:25 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz