Thema: Delphi Hook, als Componente

Einzelnen Beitrag anzeigen

4dk2

Registriert seit: 4. Sep 2007
176 Beiträge
 
#1

Hook, als Componente

  Alt 10. Mai 2010, 07:38
Hallo,

Wieder mal eine Frage zu Hooks

Mein Problem ist das ich eine Komponente machen will die einen Hook beinhaltet.

Problem dabei ist das ich die Hookfunktion nicht übergeben kann:

SetWindowsHookEx bekommt normalerweise so
HandleA := SetWindowsHookEx(WH_KEYBOARD,@Funcktion,HInstance, GetCurrentThreadId());
eine Prodedure mit.

Da es aber ja in eine Componten soll sind es ja keine einfachen procedure sondern
"Methoden" also procedure of object im Prinzip.

Ich habs sogar hinbekommen das die Procedure aufgerufen wird, wenn ich TMethod.Code übergebe,
allerdings mit Zugriffs verletzungen

Vielleicht hat einer von euch ja schonmal so was ähnliches gemacht, bzw kennt einer evtl. nen Link zu einer Komponten die Hooks benutzt.



Delphi-Quellcode:
//type TKeyboardHookProc_object=function (Code: Integer;WordParam: Word;LongParam: LongInt): LongInt of object; stdcall;
//type PKeyboardHookProc_object=^TKeyboardHookProc_object;
//type TKeyboardHookProc=function (Code: Integer;WordParam: Word;LongParam: LongInt): LongInt; stdcall;
//type PKeyboardHookProc=^TKeyboardHookProc;

type THook=class (Tcomponent)
  private
    FHooksActive:boolean;
    FHookHandle : HHook;
    function KeyboardHookProc2(Code: Integer;WordParam: Word;LongParam: LongInt): LongInt; stdcall;
  public
    procedure SetActive(const value:boolean);

end;


//.........

procedure THook.SetActive(const value:boolean);
var p:pointer;
begin
  if value=FHooksActive then exit;

  FHooksActive:=value;
  if not FHooksActive then
  begin
    UnHookWindowsHookEx(FHookHandle);
  end
  else
  begin
     p:=@KeyboardHookProc2; //hier das Problem
     FHookHandle:=SetWindowsHookEx(WH_KEYBOARD,
             {callback —>}  p, //hier muss die Funktion/Pointer zur Funktion übergeben werden
                            HInstance,
                            GetCurrentThreadId());
  end;
end;


function THook.KeyboardHookProc2(Code: Integer; WordParam: Word;
  LongParam: Integer): LongInt;
var
  shift : TShiftState;
  key : Word;
  f:Tform;
  KeyState1: TKeyBoardState;
  s:string;
begin
   result := 0;
   if Code<0 then
   begin
     result:=CallNextHookEx(FHookHandle,Code,WordParam,LongParam);
     exit;
   end;
  
   //Keydown: code=3
   //KeyUp: code=0;

   //if code<>0 then exit; // alles nur bei keyup verarbeiten...

   if (LongParam and (1 SHL 31) <> 0) then exit;




   GetKeyboardState(KeyState1);
   shift := [];
   if ((KeyState1[vk_Menu] and 128)<>0) then
     shift:=shift+[ssAlt];
   if ((KeyState1[VK_CONTROL] and 128)<>0) then
     shift:=shift+[ssCtrl];
   if ((KeyState1[VK_SHIFT] and 128)<>0) then
     shift:=shift+[ssShift];

   key := WordParam;

   if key=VK_RETURN then
       key:=0;
   if key=VK_TAB then
       key:=0;
  
   if key<>0 then
     result:=CallNextHookEx(FHookHandle,Code,WordParam,LongParam)
   else
   begin
     result:=-1;
     //CallNextHookEx(FHookHandle,Code,0,0)
   end;
end;
  Mit Zitat antworten Zitat