Delphi Build keyboard mouse action record and playback

Source: Internet
Author: User
Tags integer

Windows provides API function SetWindowsHookEx to create a hook through which a program can be added to the hook chain to monitor windows

message, the function syntax is:

SetWindowsHookEx (Idhook:integer; lpfn:tfnhookproc; hmod:hinst; dwthreadid:dword)

Where the parameter idhook specifies the type of monitoring function that is established.

With Windows MSDN Help, you can see that the SetWindowsHookEx function provides 15 different types of message monitoring, where we will use Wh_journalrecord and Wh_journalplayback to monitor keyboard and mouse operations. Parameter LPFN specifies a message function that, when the corresponding message is generated, calls the function and passes the message value to the function for processing. The general form of the function is:

HookProc (Code:integer; wparam:wparam; lparam:lparam): Lresult stdcall;

Where code is the system indicator tag, wparam and lparam are additional parameters, depending on different message monitoring types. As long as you create such a function in your program and then add it to the message monitoring chain by using the SetWindowsHookEx function, you can process the message. You need to invoke the provide UnhookWindowsHookEx to unblock the message when you do not need to monitor system messages.

The Wh_journalrecord and Wh_journalplayback types are two opposite hook types, which get a mouse, a keyboard action message, and a mouse keyboard message. So in the program we need to create two message functions, one for recording the mouse keyboard operation and saving to an array, the other to return the saved operation to the system playback.

Here's how to set up a program, create a project in Delphi, and add 3 buttons to the FORM1 for program operation. Add another button control and an edit control to verify the operation.

Here's all the code for 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//must pass the message to the next accepted unit in 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:= ' paradigm ';
Button2.enabled:=false;
Button3.enabled:=false;
End
Procedure Tform1.button1click (Sender:tobject);
Begin
eventlog:=0;
Establish a keyboard mouse operation message record chain
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 keyboard mouse operation message record playback chain
Hplay:=setwindowshookex (Wh_journalplayback,playproc,
hinstance,0);
Button3.enabled:=false;
End
End.

After the code has been added, run the program, click the "Record" button to start recording operations, then you can enter some text in the document control or click the "Example" button, and then click the "Stop" button to stop the record, and then click the "Playback" button can tell the previous action played back. In the above program, HookProc is a record operation of the message function, whenever a mouse keyboard message occurs, the system will call the function, message information is stored in the address lparam, we can speak the message saved in an array. Playproc is a message playback function that is called when the system can perform a message playback, and the program returns the previously recorded message value to the lparam point area, and the system executes the message, which enables the message playback.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.