用钩子记录分析键盘鼠标的每一个动作<br>下面的代码可以重放鼠标和键盘操作过程,希望有所帮助:<br>unit Unit1;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> Dialogs, StdCtrls, Menus;<br><br>type<br> TForm1 = class(TForm)<br> Button1: TButton;<br> Button2: TButton;<br> Button3: TButton;<br> Edit1: TEdit;<br> Button4: TButton;<br> PopupMenu1: TPopupMenu;<br> CheckBox1: TCheckBox;<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> EventArr:array[0..10000]of EVENTMSG;<br> EventLog:Integer;<br> PlayLog:Integer;<br> hHook,hPlay:Integer;<br> recOK:Integer;<br> canPlay:Integer;<br> bDelay:Bool;<br><br>implementation<br><br>{$R *.dfm}<br><br>Function PlayProc(iCode:Integer;wParam:wParam;lParam:lParam):LRESULT;stdcall;<br>begin<br> canPlay:=1;<br> Result:=0;<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> 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> if EventLog>=10000 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> 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> Button3.Enabled:=False;<br>end;<br><br>end.<br><br>object Form1: TForm1<br> Left = 192<br> Top = 107<br> Width = 544<br> Height = 375<br> Caption = 'Form1'<br> Color = clBtnFace<br> Font.Charset = DEFAULT_CHARSET<br> Font.Color = clWindowText<br> Font.Height = -11<br> Font.Name = 'MS Sans Serif'<br> Font.Style = []<br> OldCreateOrder = False<br> OnCreate = FormCreate<br> PixelsPerInch = 96<br> TextHeight = 13<br> object Button1: TButton<br> Left = 16<br> Top = 24<br> Width = 75<br> Height = 25<br> Caption = 'Button1'<br> TabOrder = 0<br> OnClick = Button1Click<br> end<br> object Button2: TButton<br> Left = 16<br> Top = 64<br> Width = 75<br> Height = 25<br> Caption = 'Button2'<br> TabOrder = 1<br> OnClick = Button2Click<br> end<br> object Button3: TButton<br> Left = 16<br> Top = 104<br> Width = 75<br> Height = 25<br> Caption = 'Button3'<br> TabOrder = 2<br> OnClick = Button3Click<br> end<br> object Edit1: TEdit<br> Left = 128<br> Top = 16<br> Width = 361<br> Height = 21<br> TabOrder = 3<br> Text = 'Edit1'<br> end<br> object Button4: TButton<br> Left = 16<br> Top = 136<br> Width = 75<br> Height = 25<br> Caption = 'Button4'<br> TabOrder = 4<br> end<br> object CheckBox1: TCheckBox<br> Left = 224<br> Top = 168<br> Width = 97<br> Height = 17<br> Caption = 'CheckBox1'<br> TabOrder = 5<br> end<br> object PopupMenu1: TPopupMenu<br> Left = 168<br> Top = 104<br> end<br>end<br>