第一步
library getKey;
uses
SysUtils
Windows
HookMain in hookmain
exports
OpenGetKeyHook
CloseGetKeyHook
GetPublicP;
begin
NextHook :=
procSaveExit := ExitProc;
DLLproc := @DLLMain;
ExitProc := @HookExit;
DLLMain(DLL_PROCESS_ATTACH);
end
第二步
unit HookMain;
interface
uses Windows
//type DataBuf = Array [
type mydata=record
data
data
end;
var hObject : Thandle;
pMem : Pointer;
NextHook: Hhook;
procSaveExit: Pointer;
function HookHandler(iCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall; export;
function OpenGetKeyHook(sender : HWND;MessageID : WORD) : BOOL; export;
function CloseGetKeyHook: BOOL; export;
function GetPublicP : Pointer;stdcall; export;
Procedure DLLMain(dwReason:Dword); far;
procedure HookExit; far;
implementation
Procedure UnMapMem;
begin
if Assigned(pMem) then
begin
UnMapViewOfFile(pMem);
pMem := Nil
end;
end;
Procedure MapMem;
begin
hObject := CreateFileMapping($FFFFFFFF
if hObject =
pMem := MapViewOfFile(hObject
//
// 創建SizeOf(DataBuf)的數據區
if not Assigned(pMem) then
begin
UnMapMem;
Raise Exception
end;
end;
Procedure DLLMain(dwReason:Dword); far;
begin
Case dwReason of
DLL_PROCESS_ATTACH :
begin
pMem := nil;
hObject :=
MapMem; //以下的公有數據
End;
DLL_PROCESS_DETACH : UnMapMem;
DLL_THREAD_ATTACH
DLL_THREAD_DETACH :; //缺省
end;
end;
procedure HookExit; far;
begin
CloseGetKeyHook;
ExitProc := procSaveExit;
end;
function GetPublicP : Pointer;export;
begin //這裡引出了公用數據區的指針
Result := pMem;
end;
function HookHandler(iCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall; export;
begin
Result :=
If iCode $#@
Then Result := CallNextHookEx(NextHook
// This is probably closer to what you would want to do
Case wparam of
WM_LBUTTONDOWN:
begin
end;
WM_LBUTTONUP:
begin
end;
WM_LBUTTONDBLCLK:
begin
end;
WM_RBUTTONDOWN:
begin
messagebeep(
end;
WM_RBUTTONUP:
begin
end;
WM_RBUTTONDBLCLK:
begin
end;
WM_MBUTTONDOWN:
begin
end;
WM_MBUTTONUP:
begin
end;
WM_MBUTTONDBLCLK:
begin
end;
WM_NCMouseMove
begin
mydata(pmem^)
// messagebeep(
//SendMessage(DataBuf(pMem^)[
SendMessage(mydata(pMem^)
end;
end; //發送消息
end;
function OpenGetKeyHook(sender : HWND;MessageID : WORD) : BOOL; export;
begin
Result := False;
if NextHook $#@
// DataBuf(pMem^)[
// DataBuf(pMem^)[
mydata(pmem^)
mydata(pmem^)
NextHook := SetWindowsHookEx(WH_mouse
Result := NextHook $#@
end;
function CloseGetKeyHook: BOOL; export;
begin
if NextHook $#@
begin
UnhookWindowshookEx(NextHook); //把鉤子鏈鏈接到下一個鉤子處理上
NextHook :=
end;
Result := NextHook =
end;
end
第三步
unit Unit
interface
uses
Windows
StdCtrls
type
TForm
uncapture: Tbutton;
capture: Tbutton;
Exit: Tbutton;
Panel
show: Tlabel;
Label
counter: Tlabel;
procedure ExitClick(Sender: Tobject);
procedure uncaptureClick(Sender: Tobject);
procedure captureClick(Sender: Tobject);
private
{ Private declarations }
public
{ Public declarations }
procedure WndProc(var Message: Tmessage); override;
end;
var
Form
var num : integer;
const MessageID = WM_User +
implementation
{$R *
function OpenGetKeyHook(sender : HWND;MessageID : WORD) : BOOL; external GetKey
function CloseGetKeyHook: BOOL; external GetKey
procedure TForm
begin
close;
end;
procedure TForm
begin
if CloseGetKeyHook then //ShowMessage(結束記錄
show
end;
procedure TForm
begin
// if OpenGetKeyHook(self
if OpenGetKeyHook(Form
//ShowMessage(開始記錄
show
num :=
end;
procedure TForm
var x
begin
if Message
begin
// Panel
x:=PMouseHookStruct( message
y:=PMouseHookStruct( message
panel
inc(Num);
counter
end
else Inherited;
end;
end
From:http://tw.wingwit.com/Article/program/Delphi/201311/24762.html