Einzelnen Beitrag anzeigen

Benutzerbild von quirks
quirks

Registriert seit: 5. Sep 2004
Ort: Fischbachtal
46 Beiträge
 
Delphi 8 Professional
 
#1

Keyboard-Hooks ja, aber global?

  Alt 8. Mai 2005, 01:53
moin moin!

habe nach langem suchen die hook-unit von assarbad gefunden, die in seinem beispiel-prog auch wunderbar global funktioniert, bei mir aber leider nur lokal. ich hab mal das ganze gepostet, inklusive meiner veränderungen, dem simplen ipc über einen client-socket.

kann mir jemand sagen, warum ich nur lokale messages bekomme?

hier noch der link zum source plus Beispiel: Library Keyboardhook


Delphi-Quellcode:
library keyboardhook;

uses
  Windows,
  Messages,
  sysutils,
  sockets;

  {$I names.inc}

var
  HookHandle: Cardinal = 0;
  WindowHandle: Cardinal = 0;
  socket: TTcpClient;

function KeyboardHookProc(nCode: Integer; wParam: WPARAM; lParam: LPARAM):
 LRESULT; stdcall;
begin
//es ist ebenfalls möglich die Bearbeitung an eine Bedingung zu knüpfen
//it's possible to call CallNextHookEx conditional only.
  Result := CallNextHookEx(HookHandle, nCode, wParam, lParam);
  case nCode < 0 of
    TRUE: exit; //wenn code kleiner 0 wird nix gemacht
                //if code smaller 0 nothing has to be done
    FALSE:
      begin
//Hier kann jetzt alles bearbeitet werden
//Here one can work with the parameters
        if assigned(socket) then begin
         socket.Sendln('nCode:'+inttostr(ncode)+',wParam:'+inttostr(wParam)+',lParam:'+inttostr(lParam));
        end;
      end;
  end;
end;

function InstallHook(Hwnd: Cardinal): Boolean; stdcall;
begin
  Result := False;
  if HookHandle = 0 then begin
//Erstmal Hook installieren
//First install the hook
    HookHandle := SetWindowsHookEx(WH_KEYBOARD, @KeyboardHookProc,
    HInstance, 0);
//Uebergebenes Fensterhandle sichern
//Save the given window handle
    WindowHandle := Hwnd;
    Result := TRUE;
    socket:=TTcpClient.Create(nil);
    socket.RemoteHost:='Localhost';
    socket.RemotePort:='27359';
    socket.Active:=true;
    socket.Sendln('active');
  end;
end;



function UninstallHook: Boolean; stdcall;
begin
//Hook aus der Hookchain entfernen
//Uninstall hook from hook chain
  Result := UnhookWindowsHookEx(HookHandle);
  HookHandle := 0;
  socket.Sendln('inactive');
  socket.Active:=false;
  socket.free;
end;

exports
//Installations- und Deinstallationsroutine exportieren
//Export the installation and deinstallation routine
  InstallHook,
  UninstallHook;
end.
  Mit Zitat antworten Zitat