=================================================单元1===========================================
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, Unit2;
const
MessageID=WM_User+100;
type
TForm1 = class(TForm)
Panel1: TPanel;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Panel2: TPanel;
GroupBox1: TGroupBox;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure WndProc(var Message:TMessage);override;
end;
var
Form1: TForm1;
num:integer;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
if OpenGetKeyHook(Form1.Handle,MessageID) then {挂接钩子过程}
Memo1.Lines.Add('开始记录');
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
if CloseGetKeyHook then {接触钩子过程挂接}
begin
Memo1.Lines.Add('结束记录');
Memo1.Lines.Add(' ');
end;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
close;
end;
procedure TForm1.WndProc(var Message:TMessage);
begin
if Message.Msg=MessageID then
begin
with Memo1.Lines do
case Message.WParam of
WM_LBUTTONDOWN:
begin
Add('在('+ IntToStr(GetX) + ','+ IntToStr(GetY ) + ')按下左键!');
end;
WM_LBUTTONUP:
begin
Add('在('+ IntToStr(GetX) + ','+ IntToStr(GetY ) + ')释放左键!');
end;
WM_LBUTTONDBLCLK:
begin
Add('在('+ IntToStr(GetX) + ','+ IntToStr(GetY ) + ')双击左键!');
end;
WM_RBUTTONDOWN:
begin
Add('在('+ IntToStr(GetX) + ','+ IntToStr(GetY ) + ')按下右键!');
end;
WM_RBUTTONUP:
begin
Add('在('+ IntToStr(GetX) + ','+ IntToStr(GetY ) + ')释放右键!');
end;
WM_RBUTTONDBLCLK:
begin
Add('在('+ IntToStr(GetX) + ','+ IntToStr(GetY ) + ')双击左键!');
end;
WM_MBUTTONDOWN:
begin
Add('在('+ IntToStr(GetX) + ','+ IntToStr(GetY ) + ')按下中键!');
end;
WM_MBUTTONUP:
begin
Add('在('+ IntToStr(GetX) + ','+ IntToStr(GetY ) + ')释放中键!');
end;
WM_MBUTTONDBLCLK:
begin
Add('在('+ IntToStr(GetX) + ','+ IntToStr(GetY ) + ')双击中键!');
end;
WM_NCMouseMove,WM_MOUSEMOVE:
begin
Add('鼠标移动到('+ IntToStr(GetX) + ','+ IntToStr(GetY ) + ')!');
end;
end;
end;
Inherited;
end;
end.
==============================================单元2==================================================
unit Unit2;
interface
uses Windows,Messages,Dialogs,SysUtils;
type
TShareData=record
data1:array[1..2] of DWORD;
data2:TMOUSEHOOKSTRUCT;
end;
const
VirtualFileName = 'ShareDllData';
DataSize = sizeof (TShareData);
var
InstalledHook:HHook;
ShareData : TShareData;
function HookHandler(iCode:Integer;wParam:WPARAM;lParam:LPARAM):
LRESULT;stdcall;export;
function OpenGetKeyHook(sender:HWND;MessageID:WORD):BOOL;
function CloseGetKeyHook:BOOL;
function GetX:integer;
function GetY:integer;
implementation
function HookHandler(iCode:Integer;wParam:WPARAM;lParam:LPARAM):LRESULT;
begin
ShareData.data2:=pMOUSEHOOKSTRUCT(lParam)^;
SendMessage(ShareData.data1[1],
ShareData.data1[2],wParam,0);
Result:=CallNextHookEx(InstalledHook,iCode,wParam,lParam);
end;
function OpenGetKeyHook(sender:HWND;MessageID:WORD):BOOL;
begin
Result:=False;
if InstalledHook = 0 then
begin
ShareData.data1[1]:=sender;
ShareData.data1[2]:=MessageID ;
InstalledHook := SetWindowsHookEx(WH_mouse,HookHandler,HInstance,0);
Result:=InstalledHook<>0;
end;
end;
function CloseGetKeyHook:BOOL;
begin
if InstalledHook<>0 then
begin
UnhookWindowshookEx(InstalledHook);
InstalledHook:=0;
end;
Result:=InstalledHook=0;
end;
function GetX:integer;
begin
GetX := ShareData.data2.pt.x;
end;
function Gety:integer;
begin
Gety := ShareData.data2.pt.y;
end;
end.