Delphi-PRAXiS
Seite 1 von 2  1 2      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi hInstance - KeyboardHook (https://www.delphipraxis.net/118049-hinstance-keyboardhook.html)

napsterxx 30. Jul 2008 15:59


hInstance - KeyboardHook
 
Moin Moin,
dachte ich beschäftige mich mit Hooks - und habe es eigentlich soweit auch verstanden, jedoch kann ich wenn ich die Tastatur hooke nur Tasten abfangen die auf meiner Form gedrückt wurden!?!?

Ich denke es liegt an hInstance im Quellcode der DLL.

DLL QuellCode:
Delphi-Quellcode:
library KeyboardHook;

uses
  Windows, SysUtils,
  Messages;

{$R *.res}

var
  HookHandle: Cardinal = 0;
  SendHwnd: Cardinal;

const
  MM_KeyDown = 2072;

function KeyboardHookProc(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
begin
  Result := CallNextHookEx( HookHandle, nCode, wParam, lParam );
  case nCode < 0 of
    True: Exit;
    False:
      begin
        SendMessage( SendHwnd, MM_KeyDown, wParam, lParam );
      end;
  end;
end;


function InstallHook( Hwnd, ResultHwnd: Cardinal ): Boolean; stdcall;
begin
  Result := False;
  if HookHandle = 0 then
  begin
    HookHandle := SetWindowsHookEx( WH_KEYBOARD, @KeyboardHookProc, hInstance, 0 );
    SendHwnd := ResultHwnd;
    Result := True;
  end;
end;

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

exports
  InstallHook,
  UninstallHook;

begin
end.
HauptProgramm QuellCode:
Delphi-Quellcode:
unit Main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure btnUninstallClick(Sender: TObject);
    procedure btnInstallClick(Sender: TObject);
  protected
    { Protected-Deklarationen }
    procedure WndProc(var Msg: TMessage); override;
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

const
  MM_KeyDown = 2072;

function InstallHook( Hwnd, ResultHwnd: THandle ): Boolean; stdcall;
  external 'KeyboardHook.dll';
function UninstallHook: Boolean; stdcall;
  external 'KeyboardHook.dll';

implementation

{$R *.dfm}

// Nachricht verarbeiten
procedure TForm1.WndProc(var Msg: TMessage);
begin
  inherited;
  if (Msg.Msg = MM_KeyDown) then
    Form1.Caption := IntToStr( Msg.WParam );
end;

// Hook installieren
procedure TForm1.btnInstallClick(Sender: TObject);
begin
  InstallHook( Handle, Handle );
end;

// Hook deinstallieren
procedure TForm1.btnUninstallClick(Sender: TObject);
begin
  UninstallHook;
end;

end.

lbccaleb 30. Jul 2008 16:05

Re: hInstance - KeyboardHook
 
Zitat:

Zitat von napsterxx
Delphi-Quellcode:
// Hook installieren
procedure TForm1.btnInstallClick(Sender: TObject);
begin
  InstallHook( Handle, Handle );
end;

du installierst den hook ja auch nur für dein hauptfenster!!
hast dus mal mit "0" probiert??

Apollonius 30. Jul 2008 16:19

Re: hInstance - KeyboardHook
 
Quatsch, der Hook wird global installiert. Das Problem ist, dass die globalen Variablen nur in einer DLL-Instanz zur Verfügung stehen. Was soll eigentlich die Variable WindowHandle?

napsterxx 30. Jul 2008 16:23

Re: hInstance - KeyboardHook
 
Die ist überflüssig :oops:
Ich editiere mal

Apollonius 30. Jul 2008 16:35

Re: hInstance - KeyboardHook
 
Um das eigentliche Problem zu behandeln, darfst du das Ziel deiner Nachrichten nicht mehr aus globalen Variablen ermitteln. Du kannst FindWindow verwenden oder deine Nachrichten an HWND_BROADCAST schicken, da gibt es allerdings ein paar Dinge zu beachten. Sehr interessant dürfte auch die Möglichkeit sein, Mailslots zu verwenden. Diese sind hier eigentlich ideal.

napsterxx 30. Jul 2008 16:40

Re: hInstance - KeyboardHook
 
Du meinst ich muss das Sendmessage verändern? Bzw. Komplett rausstreichen?

Apollonius 30. Jul 2008 16:43

Re: hInstance - KeyboardHook
 
Wie gesagt, die globale Variable hat in fast allen DLL-Instanzen den Wert 0. Du musst das Handle daher entweder dynamisch ermitteln oder einen anderen Weg gehen. Einige habe ich oben bereits aufgezeigt.

napsterxx 30. Jul 2008 16:51

Re: hInstance - KeyboardHook
 
Welche Variable? Meine Form erhält ja die gewünschte Info mit der gedrückten Taste

Apollonius 30. Jul 2008 16:55

Re: hInstance - KeyboardHook
 
Die Variable SendHwnd. Nur in der DLL-Instanz deines eigenen Programmes wird ihr ein Wert zugewiesen. In allen anderen Instanzen bleibt sie 0. Daher kommt aus diesen auch nichts an.

napsterxx 30. Jul 2008 17:19

Re: hInstance - KeyboardHook
 
Ah danke dir, nun habe ich es verstanden :D Dann werde ich wohl mir etwas anderes überlgen ^^


Alle Zeitangaben in WEZ +1. Es ist jetzt 23:56 Uhr.
Seite 1 von 2  1 2      

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