unit Unit1;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> Dialogs, StdCtrls;<br><br>type<br> TForm1 = class(TForm)<br> Button1: TButton;<br> Button2: TButton;<br> Button3: TButton;<br> Button4: TButton;<br> Edit1: TEdit;<br> Edit2: TEdit;<br> procedure FormCreate(Sender: TObject);<br> procedure Button1Click(Sender: TObject);<br> procedure Button2Click(Sender: TObject);<br> procedure Button3Click(Sender: TObject);<br> private<br> { Private declarations }<br> public<br> { Public declarations }<br> end;<br><br>var<br> Form1: TForm1;<br><br> EventArr:array[0..1000]of EVENTMSG;<br> EventLog:Integer;<br> PlayLog:Integer;<br> hHook,hPlay:Integer;<br> recOK:Integer;<br> canPlay:Integer;<br> bDelay:Bool;<br>implementation<br><br>{$R *.DFM}<br>Function PlayProc(iCode:Integer;wParam:wParam;lParam:lParam):LRESULT;stdcall;<br>begin<br> canPlay:=1;<br> Result:=0;<br><br> if iCode < 0 then //必须将消息传递到消息链的下一个接受单元<br> Result := CallNextHookEx(hPlay,iCode,wParam,lParam)<br> else if iCode = HC_SYSMODALON then<br> canPlay:=0<br> else if iCode = HC_SYSMODALOFF then<br> canPlay:=1<br> else if ((canPlay =1 )and(iCode=HC_GETNEXT)) then begin<br> if bDelay then begin<br> bDelay:=False;<br> Result:=50;<br> end;<br> pEventMSG(lParam)^:=EventArr[PlayLog];<br> end<br> else if ((canPlay = 1)and(iCode = HC_SKIP))then begin<br> bDelay := True;<br> PlayLog:=PlayLog+1;<br> end;<br> if PlayLog>=EventLog then begin<br> UNHookWindowsHookEx(hPlay);<br> end;<br>end;<br><br>function HookProc(iCode:Integer;wParam:wParam;lParam:lParam):LRESULT;stdcall;<br>begin<br> recOK:=1;<br> Result:=0;<br><br> if iCode < 0 then<br> Result := CallNextHookEx(hHook,iCode,wParam,lParam)<br> else if iCode = HC_SYSMODALON then<br> recOK:=0<br> else if iCode = HC_SYSMODALOFF then<br> recOK:=1<br> else if ((recOK>0) and (iCode = HC_ACTION)) then begin<br> EventArr[EventLog]:=pEventMSG(lParam)^;<br> EventLog:=EventLog+1;<br><br> if EventLog>=1000 then begin<br> UnHookWindowsHookEx(hHook);<br> end;<br> end;<br>end;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br> Button1.Caption:='纪录';<br> Button2.Caption:='停止';<br> Button3.Caption:='回放';<br> Button4.Caption:='范例';<br> Button2.Enabled:=False;<br> Button3.Enabled:=False;<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br> EventLog:=0;<br> //建立键盘鼠标操作消息纪录链<br> hHook:=SetwindowsHookEx(WH_JOURNALRECORD,HookProc,HInstance,0);<br> Button2.Enabled:=True;<br> Button1.Enabled:=False;<br>end;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>begin<br> UnHookWindowsHookEx(hHook);<br> hHook:=0;<br><br> Button1.Enabled:=True;<br> Button2.Enabled:=False;<br> Button3.Enabled:=True;<br>end;<br><br>procedure TForm1.Button3Click(Sender: TObject);<br>begin<br> PlayLog:=0;<br> //建立键盘鼠标操作消息纪录回放链<br> hPlay:=SetwindowsHookEx(WH_JOURNALPLAYBACK,PlayProc,<br> HInstance,0);<br><br> Button3.Enabled:=False;<br>end;<br><br>end.<br><br>