Thema: Delphi Windows-Taste abfangen

Einzelnen Beitrag anzeigen

DevilsCamp
(Gast)

n/a Beiträge
 
#6

Re: Windows-Taste abfangen

  Alt 22. Sep 2005, 13:32
Ich habe jetzt folgenden Code zur Bearbeitung eines Tastendrucks:

Erst die DLL:
Delphi-Quellcode:
library hooktest_dll;

uses
  Windows,
  Messages;

const
  keybmsg='KbHookMsg_MR';

var
  HookHandle: Cardinal = 0;
  WindowHandle: Cardinal = 0;
  WM_KEYBHOOKMSG :Cardinal = 0;

function KeyboardHookProc(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
begin
  case nCode < 0 of
    TRUE: Result := CallNextHookEx(HookHandle, nCode, wParam, lParam);
    FALSE:
      begin
        if (wParam=VK_LWIN) then
        begin
          SendMessage(HWND_BROADCAST, WM_KEYBHOOKMSG, wParam, lParam);
          Result := 0;
        end
        else
          Result := CallNextHookEx(HookHandle, nCode, wParam, lParam);
      end;
  end;
end;

function InstallHook(Hwnd: Cardinal): Boolean; stdcall;
begin
  Result := False;
  if HookHandle = 0 then begin
    HookHandle := SetWindowsHookEx(WH_KEYBOARD, @KeyboardHookProc, HInstance, 0);
    SetProp(Hwnd,keybmsg,WM_KEYBHOOKMSG);
    WindowHandle := Hwnd;
    Result := TRUE;
  end;
end;

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

exports
  InstallHook,
  UninstallHook;

begin
  WM_KEYBHOOKMSG := RegisterWindowMessage(keybmsg);
end.
Nun das Programm:
Delphi-Quellcode:
unit hooktest_main;

interface

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

const
  keybmsg='KbHookMsg_MR';

type
  TInstallHook = function(Hwnd: THandle): Boolean; stdcall;
  TUninstallHook = function: Boolean; stdcall;

  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private-Deklarationen }
  protected
    procedure WndProc(var Message: TMessage); override;
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;
  WM_KEYBHOOKMSG :Cardinal = 0;
  InstallHook: TInstallHook;
  UninstallHook: TUninstallHook;
  lib: Cardinal;
    
implementation

{$R *.dfm}

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
begin
  lib := LoadLibrary('hooktest_dll');
  if lib <> INVALID_HANDLE_VALUE then begin
    InstallHook := GetProcAddress(lib, 'InstallHook');
    UnInstallHook := GetProcAddress(lib, 'UninstallHook');
  end; // else ERROR
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  InstallHook(Self.Handle);
  WM_KEYBHOOKMSG := getprop(Self.Handle,keybmsg);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  UninstallHook;
end;

procedure TForm1.WndProc(var Message: TMessage);
begin
  if (Message.Msg=WM_KEYBHOOKMSG) then
  begin
    SetForegroundWindow(Self.Handle);
    ShowMessage(Chr(Message.WParam));
  end;
    
  inherited;
end;

end.
Aber trotzdem öffnet sich beim drücken der Windows-Taste noch das Start-Menü. Da ich die Taste aber zum Einrichten eines System-Weiten HotKeys drücken muss/will, sollte sich natürlich nicht gleich das Startmenü öffnen...
  Mit Zitat antworten Zitat