unit unit1;
{$mode delphi}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls,
Windows, MouseAndKeyInput, LCLType;
type
{ TForm1 }
TForm1 = class(TForm)
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
function Start: boolean;
function Stop: Boolean;
function IsStarted: Boolean;
{ private declarations }
public
{ public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
var
llKeyboardHook: HHOOK = 0;
bSKIP : boolean;
{ TForm1 }
function LowLevelKeyboardHook(nCode: Integer; wParam: WPARAM; lParam: LPARAM): HRESULT; stdcall;
type
PKBDLLHOOKSTRUCT = ^TKBDLLHOOKSTRUCT;
TKBDLLHOOKSTRUCT = packed record
vkCode: DWORD;
scanCode: DWORD;
flags: DWORD;
time: DWORD;
dwExtraInfo: DWORD;
end;
var
pkbhs: PKBDLLHOOKSTRUCT;
begin
pkbhs := PKBDLLHOOKSTRUCT(Pointer(lParam));
Form1.Label1.Caption := inttostr(pkbhs^.vkCode);
Form1.Label2.caption := inttostr(pkbhs^.scanCode);
Form1.Label3.Caption := inttostr(pkbhs^.dwExtraInfo);
Form1.Label4.caption := inttostr(pkbhs^.flags);
CallNextHookEx(llKeyboardHook, nCode, wParam, lParam);
end;
function TForm1.Start: boolean;
const
WH_KEYBOARD_LL = 13;
begin
if llKeyboardHook = 0 then
llKeyboardHook := SetWindowsHookEx(WH_KEYBOARD_LL, @LowLevelKeyboardHook, HInstance, 0);
Result := (llKeyboardHook <> 0)
end;
function TForm1.Stop: Boolean;
begin
Result := False;
if (llKeyboardHook <> 0) and UnhookWindowsHookEx(llKeyboardHook) then
begin
llKeyboardHook := 0;
Result := True;
end;
end;
function TForm1.IsStarted: Boolean;
begin
Result := (llKeyboardHook <> 0)
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
if IsStarted then
Stop;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
if not Start then
MessageDlg('Cannot start the hook!', mtError, [mbOk], 0)
end;
end.