Einzelnen Beitrag anzeigen

Benutzerbild von Flocke
Flocke

Registriert seit: 9. Jun 2005
Ort: Unna
1.172 Beiträge
 
Delphi 10.2 Tokyo Professional
 
#10

Re: Hook in Klasse einbinden...

  Alt 28. Aug 2005, 21:37
Eher so (auf dein erstes Beispiel angewendet, ungetestet):
Code:
TTastaturStatistik = class(TObject)
  //Tastatur
  private
    FOverAll: Int64;
    HookHandle: Cardinal;
    [b]FCallback: Pointer;[/b]
    //Set-Methoden
    procedure SetKeyHits(New: Int64);
    //Hook
    procedure Hook;
    procedure UnHook;
    [b]function LLKeyboardHookProc(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;[/b]
  public
    constructor Create;
    destructor Destroy;
    //Propertys
    property KeyHits:Int64 read FOverAll write SetKeyHits;
  end;

implementation

procedure TTastaturStatistik.Hook;
//Hook aktivieren
begin
  [b]FCallback := MakeStdcallCallback([color=red]LLKeyboardHookProc[/color]);[/b]
  HookHandle := SetWindowsHookEx(WH_KEYBOARD_LL, [b]FCallback[/b], hInstance, 0); //HIER 2
  if HookHandle = 0 then RaiseLastOSError;
end;

procedure TTastaturStatistik.UnHook;
//Hook deaktivieren
begin
  if HookHandle <> 0 then
    begin
    UnhookWindowsHookEx(HookHandle);
    HookHandle := 0;
    end;
[b] if FCallback <> nil then
  begin
    FreeCallback(FCallback);
    FCallback := nil;
  end;[/b]
end;

function [b]TTastaturStatistik[/b].LLKeyboardHookProc(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
//Wenn Taste gedrückt wird
begin
  if nCode = HC_ACTION then //lParam ist richtiger Typ
    Inc(FOverAll); //HIER 1
  Result := CallNextHookEx(HookHandle, nCode, wParam, lParam);
end;
So ist LLKeyboardHookProc eine Methode deiner Klasse und du kannst Self usw. darin benutzen.

Bei der roten Stelle bin ich mir nicht sicher, ob da ein Cast herum muss.
Volker
Besucht meine Garage
Aktuell: RtfLabel 1.3d, PrintToFile 1.4
  Mit Zitat antworten Zitat