| |

VerySource

 Forgot password?
 Register
Search
Author: waeijill

Ask how to record and play back mouse events (I have just come into contact with vc students for two weeks)

[Copy link]

0

Threads

14

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

Post time: 2020-2-9 11:15:02
| Show all posts
There are three steps to writing a hook program: defining a hook function, installing a hook, and uninstalling a hook.
  1. Defining hook functions
Hook function is a special callback function. After specific events monitored by the hook occur, the system will call the hook function for processing. The form of the hook function is different for different events. Take the mouse hook function as an example to illustrate the prototype of the hook function:
LRESULT CALLBACK HookProc (int nCode, WPARAM wParam, LPARAM lParam)
The parameters wParam and lParam contain information about the hooked message, such as mouse position, status, keyboard keys, etc. nCode contains information about the message itself, such as whether it was removed from the message queue.
We first implement the custom function in the hook function, and then call the function CallNextHookEx. To pass the hook information to the next hook function of the hook chain. The prototype of CallNextHookEx. Is as follows:
LRESULT CallNextHookEx (HHOOK hhk, int nCode, WPARAM wParam, LPARAM lParam
The parameter hhk is the hook handle. nCode, wParam, and lParam are hook functions.
Of course, the message can also be discarded by returning TRUE directly, which prevents the message from being delivered.
2. Mounting hook
程序 When the program is initialized, the function SetWindowsHookEx is called to install the hook. Its function prototype is:
HHOOK SetWindowsHookEx (int idHook, HOOKPROC lpfn, INSTANCE hMod, DWORD dwThreadId
The parameter idHook represents the hook type, which corresponds to the hook function type one-to-one. For example, WH_KEYBOARD indicates that a keyboard hook is installed, WH_MOUSE indicates a mouse hook, and so on.
Lpfn is the address of the hook function.
ModHMod is the handle of the instance where the hook function is located. For thread hooks, this parameter is NULL; for system hooks, this parameter is the DLL handle where the hook function is located.
DwThreadId specifies the thread number of the thread monitored by the hook. For global hooks, this parameter is NULL.
SetWindowsHookEx returns the handle of the installed hook.
3. Uninstall hook
When the hook is no longer used, it must be uninstalled in time. Simply call the function BOOL UnhookWindowsHookEx (HHOOK hhk).
Reply

Use magic Report

0

Threads

14

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

Post time: 2020-2-14 00:45:01
| Show all posts
Make good use of search engines and make good use of sites like codeproject and codeguru.
Reply

Use magic Report

1

Threads

14

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

 Author| Post time: 2020-4-26 22:45:01
| Show all posts
Only thanks!
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-7-27 12:45:01
| Show all posts
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.
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-9-4 14:15:01
| Show all posts
MARK
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list