I. sendinput
Sendinput can insert the specified mouse and keyboard message to the system message queue to simulate the mouse and keyboard. There are manyProgramSendinput is blocked, but not all. So here we will introduce the use of sendinput. I have written the main simulation function in a cell file: simousekeyboard. Pas. by calling the functions in the cell file, you can simulate the mouse and keyboard. For the download of this unit file, see the end of this building. The sendinput parameter is actually very simple. The declaration of a function in windows. PAS is as follows:
Function sendinput (cinputs: uint; var pinputs: tinput; cbsize: integer): uint; stdcall;
Cinputs: defines the number of elements in the record array in pinputs. Pinputs: 1st elements of the tinput record array. Each element represents a keyboard or mouse event that inserts a person into the system message queue. Cbsize: defines the tinput size, generally sizeof (tinput ). The function returns the number of events successfully inserted into the system message queue. If the function fails, 0 is returned. The key to calling sendinput is to clarify the meaning of several record structures. The declaration of tinput in windows. PAS is as follows:
Taginput = packed record
Itype: DWORD;
Case INTEGER
0: (MI: tmouseinput );
1: (KI: tkeybdinput );
2: (Hi: thardwareinput );
End;
Tinput = taginput;
Among them, mi, KI, and HI are three common record structures. itype indicates the types used in the record structure, and it has three values. Input_mouse: indicates that the MI record structure is used, and Ki and HI are ignored; input_keyboard: indicates that the ki record structure is used, and MI and HI are ignored.
2. keyboard Simulation
The tkeybdinput record structure statement is as follows:
Tagkeybdinput = packed record
Wvk: word;
Wscan: word;
Dwflags: DWORD;
Time: DWORD;
Dwextrainfo: DWORD;
End;
Tkeybdinput = tagkeybdinput;
Wvk is the virtual key code of the key to be operated. Wscan is a security code, which is generally not used. Dwflags specifies the operation performed on the keyboard. If it is 0, a key is pressed, and keyeventf_keyup indicates that a key is released. Time is the timestamp. You can use the API function gettickcount to return the value. Dwextrainfo is the extension information. You can use the API function getmessageextrainfo to return the value. For example, the program to press "a" is as follows:
Procedure keypressa;
VaR
Inputs: array [0 .. 1] of tinput;
Begin
Inputs [0]. itype: = input_keyboard;
With inputs [0]. Ki Do
Begin
Wvk: = vk_a;
Wscan: = 0;
Dwflags: = 0;
Time: = gettickcount;
Dwextrainfo: = getmessageextrainfo;
End;
Inputs [1]. itype: = input_keyboard;
With inputs [1]. Ki Do
Begin
Wvk: = vk_a;
Wscan: = 0;
Dwflags: = keyeventf_keyup;
Time: = gettickcount;
Dwextrainfo: = getmessageextrainfo;
End;
Sendinput (2, inputs [0], sizeof (tinput ));
End;
Note: in windows. the PAS unit does not contain letters or numbers. the PAS unit file re-declares all the virtual key codes, including letters, numbers, and punctuation marks.
3. Mouse Simulation
The tmouseinput record structure statement is as follows:
Tagmouseinput = packed record
DX: longint;
DY: longint;
Mousedata: DWORD;
Dwflags: DWORD;
Time: DWORD;
Dwextrainfo: DWORD;
End;
Tmouseinput = tagmouseinput;
DX and Dy indicate the Coordinate Difference (not the pixel unit) when the mouse moves, which is valid when the mouse moves. Mousedata is the scroll value of the mouse wheel, which is valid when you scroll the mouse wheel. When mousedata is smaller than 0, scroll down. When mousedata is greater than 0, scroll up. The absolute value of mousedata is set to 120. Dwflags specifies the operation performed by the mouse. For example, mouseeventf_move indicates moving the mouse, mouseeventf_leftdown indicates pressing the left mouse button, and mouseeventf_leftup indicates releasing the left mouse button. Time is the timestamp. You can use the API function gettickcount to return the value. Dwextrainfo is the extension information. You can use the API function getmessageextrainfo to return the value. For example, the Program for clicking the left mouse button is as follows:
Procedure mouseclick;
VaR
Inputs: array [0 .. 1] of tinput;
Begin
Inputs [0]. itype: = input_mouse;
With inputs [0]. Mi Do
Begin
DX: = 0;
DY: = 0;
Mousedata: = 0;
Dwflags: = mouseeventf_leftdown;
Time: = gettickcount;
Dwextrainfo: = getmessageextrainfo;
End;
Inputs [1]. itype: = input_mouse;
With inputs [1]. Mi Do
Begin
DX: = 0;
DY: = 0;
Mousedata: = 0;
Dwflags: = mouseeventf_leftup;
Time: = gettickcount;
Dwextrainfo: = getmessageextrainfo;
End;
Sendinput (2, inputs [0], sizeof (tinput ));
End;
moving the mouse is always troublesome. The DX and Dy above are not measured in pixels, but in the unit of moving the mouse device, the ratio between them is affected by the speed setting of the mouse movement. I have discussed the specific solution in the article "using winio to simulate the mouse and keyboard details under Delphi". I will not repeat it here. Dwflags can be used to set a mouseeventf_absolute flag, which allows you to move the mouse in another way. When the mouseeventf_absolute flag is set for dwflags, Dx and Dy are the screen coordinate values, which indicates moving the mouse to the Dx and Dy positions. However, this coordinate value is not measured in pixels. The value ranges from 0 to 65535 ($ FFFF). When dx is equal to 0 and Dy is equal to 0, it indicates the top left corner of the screen, when dx is 65535 or Dy is 65535, it indicates the bottom right corner of the screen, which is equivalent to dividing the width and height of the screen by 65536. The API function getsystemmetrics (sm_cxscreen) can return the screen width. The function getsystemmetrics (sm_cyscreen) can return the screen height, the screen width and height can be used to convert the pixel coordinates into the corresponding Dx and Dy. Note: This conversion produces a maximum error of 1 pixel. For example, the procedure for moving the mouse pointer to the 150,120 coordinate of the screen is as follows:
Procedure mousemove;
VaR
Input: tinput;
Begin
Input. itype: = input_mouse;
With input. Mi Do
Begin
DX: = ($ FFFF Div (getsystemmetrics (sm_cxscreen)-1) * 150;
DY: = ($ FFFF Div (getsystemmetrics (sm_cyscreen)-1) * 120;
Mousedata: = 0;
Dwflags: = mouseeventf_move or mouseeventf_absolute;
Time: = gettickcount;
Dwextrainfo: = getmessageextrainfo;
End;
Sendinput (1, input, sizeof (tinput ));
End;
Iv. Comparison of sendinput and winio
I have mentioned many disadvantages of winio in the article "details on simulating the mouse and keyboard using winio in Delphi". sendinput has almost no such disadvantages. Sendinput simulation is much easier than winio. The event is directly inserted into the system message queue, so it is faster than winio. The system also ensures data integrity and prevents data packet confusion. You can use "absolute movement" to move the mouse pointer to an accurate position. The configuration of the same mouse is isolated without compatibility issues. The disadvantage of sendinput is the worst, and it will be blocked by some programs. Therefore, if both sendinput and winio can be used, sendinput is given priority. In addition, sendinput and winio can be used in combination. Some programs are sensitive to the left mouse click. You can use winio to simulate the left mouse click. Other operations are simulated by sendinput.
V. Use of simousekeyboard. Pas
I re-declare all the virtual key codes in the simousekeyboard. Pas unit file, including letters, numbers, and punctuation that are not declared in the windows. Pas unit. There are a total of nine functions in the simousekeyboard. Pas unit file. The usage is as follows:
1. Procedure sikeydown (key: Word );
Press the specified key. The key is a virtual key.
2. Procedure sikeyup (key: Word );
Release the specified key. The key is a virtual key.
3. Procedure sikeypress (key: word; interval: Cardinal );
Press and release the specified key. interval is the interval between the press and release. Note: This function does not support repeat, that is, no matter how many Interval Settings have only one button.
4. Procedure sikeyinput (const text: string; interval: Cardinal );
Input the specified text on the keyboard to check whether the result is successful. Only single-byte characters (#32 ~ #126), tab (#9), and enter key (#13), which are characters that can be entered on the keyboard, not Chinese characters. Other characters are ignored. Interval is the interval between pressing and releasing a key, in milliseconds.
For the demo program, press Ctrl + A as follows:
Sikeydown (vk_control); // press Ctrl
Sikeypress (vk_a); // press Key
Sikeyup (vk_control); // open CTRL
5. Procedure simousedown (key: Word );
Press the specified key. The key is a virtual key code, the left mouse button is vk_lbutton, the right-click vk_rbutton, and the middle-click vk_mbutton.
6. Procedure simouseup (key: Word );
Open the specified mouse key. The key is a virtual key code, the left mouse button is vk_lbutton, the right-click vk_rbutton, and the middle-click vk_mbutton.
7. Procedure simouseclick (key: word; interval: Cardinal );
Click the specified key, and interval is the interval between pressing and releasing. Too fast clicking may make some programs unrecognizable. Adjusting the value of interval can solve this problem.
8. Procedure simousewheel (Dz: integer );
Scroll the scroll wheel of the mouse. When DZ is smaller than 0, scroll down. When DZ is greater than 0, scroll up. The absolute value of DZ is set to 120.
9. Procedure simousemoveto (X, Y: integer; maxmove: integer; interval: Cardinal );
Move the mouse pointer to a specified position and return whether the operation is successful. X and Y are pixel values. The range of X and Y cannot exceed the screen. maxmove is the maximum value of DX and Dy when moving, and interval is the interval between two moves, some programs are sensitive to mouse movement. When the mouse moves too fast, it cannot respond to the mouse. Setting maxmove and interval values properly can solve this problem.
The demo program is dragged to the specified position as follows:
Simousedown (vk_lbutton); // press the left mouse button
Simousemoveto (780,300); // move to the specified position
Simouseup (vk_lbutton); // open the left mouse button