|
The hook technology mentioned above is already available. Below is a section of the original code written by delphi, which can be completely passed in delphi. VC and its principle and operation are basically the same. I will send it to you for your reference.
Windows provides API function SetwindowsHookEx to create a Hook, through this function you can add a program to the Hook chain to monitor Windows
Message, the function syntax is:
SetWindowsHookEx(idHook: Integer; lpfn: TFNHookProc; hmod: HINST; dwThreadId: DWORD)
The parameter idHook specifies the type of monitoring function to be established. Through Windows MSDN help, you can see that the SetwindowsHookEx function provides 15 different
Message monitoring type, here we will use WH_JOURNALRECORD and WH_JOURNALPLAYBACK to monitor keyboard and mouse operations. The parameter lpfn specifies the elimination
After the corresponding message is generated, the system will call the function and pass the message value to the function for processing. The general form of the function is:
Hookproc (code: Integer; wparam: WPARAM; lparam: LPARAM): LRESULT stdcall;
The code is the system indicator, and wParam and lParam are additional parameters, which differ according to different message monitoring types. Just create this in the program
A function can process the message by adding it to the message monitoring chain through the SetwindowsHookEx function.
When you do not need to monitor system messages, you need to call UnHookWindowsHookEx to release the monitoring of messages.
The WH_JOURNALRECORD and WH_JOURNALPLAYBACK types are two opposite Hook types. The former gets mouse and keyboard action messages, and the latter plays back the mouse.
Mark keyboard messages. So in the program we need to create two message functions, one is used to record mouse and keyboard operations and saved to an array, the other is used
Return the saved operation to the system for playback.
Let's create a program, create a project in Delphi, and add 3 buttons on Form1 for program operation. In addition, add a button control and a
An Edit control is used to verify operations.
Below is the entire code of Form1
unit Unit1;
interface
uses
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
{$R *.DFM}
Function PlayProc(iCode:Integer;wParam:wParam;lParam:lParam):LRESULT;stdcall;
begin
canPlay:=1;
Result:=0;
if iCode <0 then //The message must be delivered to the next receiving unit of the message chain
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 begin
UNHookWindowsHookEx(hPlay);
end;
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:='Record';
Button2.Caption:='Stop';
Button3.Caption:='Playback';
Button4.Caption:='Example';
Button2.Enabled:=False;
Button3.Enabled:=False;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
EventLog:=0;
//Create a message record chain for keyboard and mouse operations
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;
//Establish the keyboard and mouse operation message record playback chain
hPlay:=SetwindowsHookEx(WH_JOURNALPLAYBACK,PlayProc,
HInstance,0);
Button3.Enabled:=False;
end;
end.
After adding the code, run the program and click the "Record" button to start the recording operation. At this time, you can enter some text in the text control or click
"Example" button, and then click the "Stop" button to stop recording, and then click the "Playback" button to replay the previous operations.
In the above program, HookProc is a message function for recording operations. Whenever a mouse and keyboard message occurs, the system will call this function.
The information is stored in the address lParam, we can say that the information is stored in an array. PlayProc is a message playback function, when the system can perform message playback
When this function is called, the program will return the previously recorded message value to the area pointed to by lParam, and the system will execute the message, thereby realizing message playback. |
|