unit Unit1;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Edit1: TEdit;
Button4: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;EventArr:array[0..1000]of EVENTMSG;
EventLog:Integer;
PlayLog:Integer;
hHook,hPlay:Integer;
recOK:Integer;
canPlay:Integer;
bDelay:Bool;
implementation
{typedef struct tagEVENTMSG { // em
UINT message;
UINT paramL;
UINT paramH;
DWORD time;
HWND hwnd;
}// EVENTMSG;}{$R *.DFM}
Function PlayProc(iCode:Integer;wParam:wParam;lParam:lParam):LRESULT;stdcall;
begin
canPlay:=1;
Result:=0;if iCode < 0 then //必须将消息传递到消息链的下一个接受单元
Result := CallNextHookEx(hPlay,iCode,wParam,lParam)
else if iCode = HC_SYSMODALON then
canPlay:=0
else if iCode = HC_SYSMODALOFF then
canPlay:=1
else if ((canPlay =1 )and(iCode=HC_GETNEXT)) then
begin
if bDelay then
begin
bDelay:=False;
Result:=50;
end;
pEventMSG(lParam)^:=EventArr[PlayLog];
end
else if ((canPlay = 1)and(iCode = HC_SKIP))then
begin
bDelay := True;
PlayLog:=PlayLog+1;
end;
if PlayLog>=EventLog then UNHookWindowsHookEx(hPlay);end;function HookProc(iCode:Integer;wParam:wParam;lParam:lParam):LRESULT;stdcall;
begin
recOK:=1;
Result:=0; if iCode < 0 then
Result := CallNextHookEx(hHook,iCode,wParam,lParam) //执行当前钩子,
else if iCode = HC_SYSMODALON then
recOK:=0
else if iCode = HC_SYSMODALOFF then
recOK:=1
else if ((recOK>0) and (iCode = HC_ACTION)) then
begin
EventArr[EventLog]:=pEventMSG(lParam)^;
EventLog:=EventLog+1;
if EventLog >=1000 then
begin
UnHookWindowsHookEx(hHook);
end;
end;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
Button1.Caption:='纪录';
Button2.Caption:='停止';
Button3.Caption:='回放';
Button4.Caption:='范例';
//Button2.Enabled:=False;
//Button3.Enabled:=False;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
EventLog:=0;
//建立键盘鼠标操作消息纪录链
hHook:=SetwindowsHookEx(WH_JOURNALRECORD,HookProc,HInstance,0);
Button2.Enabled:=True;
Button1.Enabled:=False;
end;procedure TForm1.Button2Click(Sender: TObject);
begin
UnHookWindowsHookEx(hHook); //停止 释放钩子
hHook:=0; //
Button1.Enabled:=True;
Button2.Enabled:=False;
Button3.Enabled:=True;
end;procedure TForm1.Button3Click(Sender: TObject);
begin
PlayLog:=0;
//建立键盘鼠标操作消息纪录回放链
hPlay:=SetwindowsHookEx(WH_JOURNALPLAYBACK,PlayProc,HInstance,0);
Button3.Enabled:=False;
{HHOOK SetWindowsHookEx( int idHook, // type of hook to install 钩子类型 比如这里的WH_journalplayback,就是回放存储的信息过程
HOOKPROC lpfn, // address of hook procedure 钩子处理过程,hookProc 是一个回调过程,需要自己写,这里是指playpro过程
HINSTANCE hMod, // handle of application instance 实例句柄。DefHookProc
DWORD dwThreadId // identity of thread to install hook for
);}
end;end.
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Edit1: TEdit;
Button4: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;EventArr:array[0..1000]of EVENTMSG;
EventLog:Integer;
PlayLog:Integer;
hHook,hPlay:Integer;
recOK:Integer;
canPlay:Integer;
bDelay:Bool;
implementation
{typedef struct tagEVENTMSG { // em
UINT message;
UINT paramL;
UINT paramH;
DWORD time;
HWND hwnd;
}// EVENTMSG;}{$R *.DFM}
Function PlayProc(iCode:Integer;wParam:wParam;lParam:lParam):LRESULT;stdcall;
begin
canPlay:=1;
Result:=0;if iCode < 0 then //必须将消息传递到消息链的下一个接受单元
Result := CallNextHookEx(hPlay,iCode,wParam,lParam)
else if iCode = HC_SYSMODALON then
canPlay:=0
else if iCode = HC_SYSMODALOFF then
canPlay:=1
else if ((canPlay =1 )and(iCode=HC_GETNEXT)) then
begin
if bDelay then
begin
bDelay:=False;
Result:=50;
end;
pEventMSG(lParam)^:=EventArr[PlayLog];
end
else if ((canPlay = 1)and(iCode = HC_SKIP))then
begin
bDelay := True;
PlayLog:=PlayLog+1;
end;
if PlayLog>=EventLog then UNHookWindowsHookEx(hPlay);end;function HookProc(iCode:Integer;wParam:wParam;lParam:lParam):LRESULT;stdcall;
begin
recOK:=1;
Result:=0; if iCode < 0 then
Result := CallNextHookEx(hHook,iCode,wParam,lParam) //执行当前钩子,
else if iCode = HC_SYSMODALON then
recOK:=0
else if iCode = HC_SYSMODALOFF then
recOK:=1
else if ((recOK>0) and (iCode = HC_ACTION)) then
begin
EventArr[EventLog]:=pEventMSG(lParam)^;
EventLog:=EventLog+1;
if EventLog >=1000 then
begin
UnHookWindowsHookEx(hHook);
end;
end;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
Button1.Caption:='纪录';
Button2.Caption:='停止';
Button3.Caption:='回放';
Button4.Caption:='范例';
//Button2.Enabled:=False;
//Button3.Enabled:=False;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
EventLog:=0;
//建立键盘鼠标操作消息纪录链
hHook:=SetwindowsHookEx(WH_JOURNALRECORD,HookProc,HInstance,0);
Button2.Enabled:=True;
Button1.Enabled:=False;
end;procedure TForm1.Button2Click(Sender: TObject);
begin
UnHookWindowsHookEx(hHook); //停止 释放钩子
hHook:=0; //
Button1.Enabled:=True;
Button2.Enabled:=False;
Button3.Enabled:=True;
end;procedure TForm1.Button3Click(Sender: TObject);
begin
PlayLog:=0;
//建立键盘鼠标操作消息纪录回放链
hPlay:=SetwindowsHookEx(WH_JOURNALPLAYBACK,PlayProc,HInstance,0);
Button3.Enabled:=False;
{HHOOK SetWindowsHookEx( int idHook, // type of hook to install 钩子类型 比如这里的WH_journalplayback,就是回放存储的信息过程
HOOKPROC lpfn, // address of hook procedure 钩子处理过程,hookProc 是一个回调过程,需要自己写,这里是指playpro过程
HINSTANCE hMod, // handle of application instance 实例句柄。DefHookProc
DWORD dwThreadId // identity of thread to install hook for
);}
end;end.
HC_GETNEXT
The hook procedure must copy the current mouse or keyboard message to the EVENTMSG structure pointed to by the lParam parameter. HC_NOREMOVE
An application has called the PeekMessage function with wRemoveMsg set to PM_NOREMOVE, indicating that the message is not removed from the message queue after PeekMessage processing.
HC_SKIP
The hook procedure must prepare to copy the next mouse or keyboard message to the EVENTMSG structure pointed to by lParam. Upon receiving the HC_GETNEXT code, the hook procedure must copy the message to the structure. HC_SYSMODALOFF
A system-modal dialog box has been destroyed. The hook procedure must resume playing back the messages. HC_SYSMODALON
A system-modal dialog box is being displayed. Until the dialog box is destroyed, the hook procedure must stop playing back messages.