In the big line of Windows today, Windows interface program by the majority of users welcome. There are two kinds of operation for these programs, keyboard input control and mouse input control. Sometimes, for complex, or repetitive operations, can we compile programs instead of manual input and use programs to simulate keyboard and mouse input? The answer is yes. This is achieved primarily through two API functions.
The following is an example of Delphi to explain how to achieve these two functions. Analog keyboard We use Keybd_event This API function, simulates the mouse button with the Mouse_event function. You don't have to worry about it, it's convenient to call the API function in Delphi.
Let's introduce the keybd_event function. Keybd_event can trigger a keystroke event, which means that a wm_keydown or WM_KEYUP message is generated back. Of course, the two messages can be used to simulate the keys, but not directly with this function convenient. Keybd_event A total of four parameters, the first key is the virtual key value, such as the ENTER key is Vk_return, tab is Vk_tab. The second parameter is a scan code, usually not set, with 0 instead of the line. The third parameter is the option flag, if the KeyDown is 0, if the KEYUP is set to "Keyeventf_keyup", the fourth parameter is generally 0. The following code can be implemented to simulate pressing the I key, wherein the $49 represents the I key virtual key value:
keybd_event ($49,0,0,0);
keybd_event ($49,0,keyeventf_keyup,0); ...
Mouse_event preferably with the Setcursorpos (x, y) function, similar to keybd_event, Mouse_event has five parameters, the first is the option flag, the Mouseeventf_leftdown when the left key is pressed, the Mouseeventf_leftup indicates that the left button is released and sends the appropriate message to the system. The 23rd parameter represents x, Y relative position, can be set to 0, 0, 45th parameter is not important, generally can also be set to 0, 0. To get more detailed usage of the keybd_event and mouse_event functions, you can consult the MSDN or Delphi Help. Here's a sample code for Mouse_event:
Setcursorpos (20,132);
Mouse_event (mouseeventf_leftdown,0,0,0,0);
Mouse_event (mouseeventf_leftup,0,0,0,0);
Mouse_event (mouseeventf_leftdown,0,0,0,0);
Mouse_event (mouseeventf_leftup,0,0,0,0); ...
The above code indicates a double-click of the mouse, to indicate that you clicked, with two mouse_event (one drop, one release at a time).
Note, whether it is the simulation keyboard or mouse events, you should pay attention to restore, that is, press the key to release, a KeyDown corresponding to a keyup; Click the mouse to release, or it may affect the function of the program.
The analog keyboard input first uses an API function: keybd_event.
We are rookie, so do not have to specifically understand its detailed usage, as long as the following methods to use it! Oh!
The analog button has two basic actions, namely pressing the key and releasing the key, so we call the API function twice each time we simulate the button, the method is:
Example 1: Simulate pressing the ' A ' key
keybd_event (65,0,0,0);
keybd_event (65,0,keyeventf_keyup,0);
Example 2: Analog Press the ' ALT+F4 ' key
keybd_event (18,0,0,0);
keybd_event (115,0,0,0);
keybd_event (115,0,keyeventf_keyup,0);
keybd_event (18,0,keyeventf_keyup,0);
Attached: Key value comparison table of common analog keys.
Keyboard key and Virtual key code comparison table
Keys for alphanumeric keypad key function keys other keys
Key code key keys key code keys key code
A 0 F1 Backspace 8
B 1 F2 113 Tab 9
C 2 98 F3 Clear 12
D-3 F4-13
E 4 F5-Shift 16
F 5 101 F6 117 Control 17
G 6 102 F7 118 Alt 18
H 7 103 F8 119 caps Lock 20
I 8 104 F9 27 ESC
J 9 F10 121 Spacebar 32
K * 106 F11 122 Page up 33
L + 107 F12 123 Page down 34
M + Enter 108----End 35
N 78-109----Home 36
O 79. ----left Arrow 37
P 80/111----up Arrow 38
Q Bayi--------Right Arrow 39
R--------down Arrow 40
S---------Insert 45
T--------Delete 46
U---------help 47
V--------Num Lock 144
W 87
X 88
Y 89
Z 90
0 48
1 49
2 50
3 51
4 52
5 53
6 54
7 55
8 56
9 57
Well, I hope this article will let you have a preliminary understanding of the keyboard and mouse buttons, if you want to know more about the mystery, you can check the detailed MSDN online Help, and a lot of hands-on practice.
Attached: A more detailed list of key values:
Constant name |
Hexadecimal value |
Decimal value |
corresponding keys |
Vk_lbutton |
01 |
1 |
The left mouse button |
Vk_rbutton |
02 |
2 |
The right mouse button |
Vk-cancel |
03 |
3 |
Ctrl+break (usually no processing required) |
Vk_mbutton |
04 |
4 |
Middle mouse button (three-button mouse) |
Vk_back |
08 |
8 |
Backspace key |
Vk_tab |
09 |
9 |
TAB key |
Vk_clear |
0C |
12 |
Clear key (number pad when Num lock is off 5) |
Vk_return |
0D |
13 |
Enter key |
Vk_shift |
10 |
16 |
Shift key |
Vk_control |
11 |
17 |
CTRL key |
Vk_menu |
12 |
18 |
ALT key |
Vk_pause |
13 |
19 |
Pause key |
Vk_capital |
14 |
20 |
Caps Lock key |
Vk_escape |
1 b |
27 |
ESE key |
Vk_space |
20 |
32 |
SPACEBAR key |
Vk_prior |
21st |
33 |
Page up key |
Vk_next |
22 |
34 |
Page DOMW Key |
Vk_end |
23 |
35 |
End key |
Vk_home |
24 |
36 |
Home key |
Vk_left |
25 |
37 |
Left ARROW key (←) |
Vk_up |
26 |
38 |
Up ARROW key (↑) |
Vk_right |
27 |
39 |
Right ARROW key (→) |
Vk_down |
28 |
40 |
Down ARROW key (↓) |
Vk_select |
29 |
41 |
Select key |
Vk_print |
2 A |
42 |
|
Vk_execute |
2 b |
43 |
Execute key |
Vk_snapshot |
2 C |
44 |
Print screen key (capture) |
Vk_insert |
The |
45 |
Ins key (number pad when Num lock is off 0) |
Vk_delete |
2E |
46 |
Del key (number pad when Num lock is off). |
Vk_help |
2F |
47 |
Help key |
Vk_0 |
30 |
48 |
0 keys |
Vk_1 |
31 |
49 |
1 keys |
Vk_2 |
32 |
50 |
2 keys |
Vk_3 |
33 |
51 |
3 keys |
Vk_4 |
34 |
52 |
4 keys |
Vk_5 |
35 |
53 |
5 keys |
Vk_6 |
36 |
54 |
6 keys |
Vk_7 |
37 |
55 |
7 keys |
Vk_8 |
38 |
56 |
8 keys |
Vk_9 |
39 |
57 |
9 keys |
Vk_a |
41 |
65 |
A key |
Vk_b |
42 |
66 |
B Key |
Vk_c |
43 |
67 |
C Key |
Vk_d |
44 |
68 |
D Key |
Vk_e |
45 |
69 |
E key |
Vk_f |
46 |
70 |
F key |
Vk_g |
47 |
71 |
G Key |
Vk_h |
48 |
72 |
H key |
Vk_i |
49 |
73 |
I key |
Vk_j |
4 A |
74 |
J Key |
Vk_k |
4 b |
75 |
K Key |
vk_l |
4C |
76 |
L Key |
Vk_m |
4D |
77 |
M key |
Vk_n |
4E |
78 |
N Key |
Vk_o |
4F |
79 |
O Key |
Vk_p |
50 |
80 |
P Key |
Vk_q |
51 |
81 |
Q Key |
Vk_r |
52 |
82 |
R key |
vk_s |
53 |
83 |
S key |
vk_t |
54 |
84 |
T key |
Vk_u |
55 |
85 |
U key |
Vk_v |
56 |
86 |
V Key |
Vk_w |
57 |
87 |
W Key |
Vk_x |
58 |
88 |
X key |
Vk_y |
59 |
89 |
Y key |
Vk_z |
5 A |
90 |
Z Key |
Vk_numpad0 |
60 |
96 |
Number keys 0 keys |
Vk_numpad1 |
61 |
97 |
Number keys 1 keys |
Vk_numpad2 |
62 |
98 |
Number keys 2 keys |
Vk_numpad3 |
62 |
99 |
Number keys 3 keys |
Vk_numpad4 |
64 |
100 |
Number keys 4 keys |
Vk_numpad5 |
65 |
101 |
Number keys 5 keys |
Vk_numpad6 |
66 |
102 |
Number keys 6 keys |
Vk_numpad7 |
67 |
103 |
Number keys 7 keys |
Vk_numpad8 |
68 |
104 |
Number keys 8 keys |
Vk_numpad9 |
69 |
105 |
Number keys 9 keys |
Vk_multiply |
6A |
106 |
The * key on the numeric keypad |
Vk_add |
6B |
107 |
The + key on the numeric keypad |
Vk_separator |
6C |
108 |
Separator key |
Vk_subtract |
6D |
109 |
The-key on the numeric keypad |
Vk_decimal |
6E |
110 |
The. Key on the numeric keypad |
Vk_divide |
6F |
111 |
/keys on the numeric keypad |
Vk_f1 |
70 |
112 |
F1 Key |
Vk_f2 |
71 |
113 |
F2 Key |
Vk_f3 |
72 |
114 |
F3 Key |
Vk_f4 |
73 |
115 |
F4 Key |
Vk_f5 |
74 |
116 |
F5 Key |
Vk_f6 |
75 |
117 |
F6 Key |
Vk_f7 |
76 |
118 |
F7 Key |
Vk_f8 |
77 |
119 |
F8 Key |
Vk_f9 |
78 |
120 |
F9 Key |
Vk_f10 |
79 |
121 |
F10 Key |
Vk_f11 |
7A |
122 |
F11 Key |
Vk_f12 |
7B |
123 |
F12 Key |
Vk_numlock |
90 |
144 |
Num Lock Key |
Vk_scroll |
91 |
145 |
Scroll Lock key |
|
|
|
|
Not mentioned above: (both on the big keyboard) |
|
|
|
Vk_lwin |
|
91 |
Left Win key |
Vk_rwin |
|
92 |
Right Win key |
Vk_apps |
|
93 |
Right Ctrl left button, click the equivalent right mouse button, will pop up the shortcut menu |
|
|
186 |
;(semicolon) |
|
|
187 |
= key |
|
|
188 |
, key (comma) |
|
|
189 |
-Key (minus) |
|
|
190 |
. Key (Period) |
|
|
191 |
/Key |
|
|
192 |
' Key (ESC below) |
|
|
219 |
[Key |
|
|
220 |
\ Key |
|
|
221 |
] Key |
|
|
222 |
' Key (quotation marks) |
Delphi Virtual Key Correspondence key
Vk_lbutton left mouse button
Vk_rbutton right mouse button
Vk_cancel Control + Rest
Vk_mbutton Middle mouse button
Vk_back backspace key
Vk_tab TAB Key
Vk_clear Clear the main
Vk_return Enter key
Vk_shift SHIFT Key
Vk_control CTRL key
Vk_menu ALT key
Vk_pause Pause Key
Vk_capital CAPS LOCK key
Vk_kana can be used with input method
Vk_hangul can be used with input method
Vk_junja can be used with input method
Vk_final can be used with input method
Vk_hanja can be used with input method
Vk_kanji can be used with input method
Vk_convert can be used with input method
Vk_nonconvert can be used with input method
Vk_accept can be used with input method
Vk_modechange can be used with input method
Vk_escape ESC key
Vk_space Space Bar
Vk_prior Page Key
Vk_next Next Page key
Vk_end END Key
Vk_home Homepage Key
Vk_left LEFT ARROW key
Vk_up up key
Vk_right RIGHT ARROW key
Vk_down DOWN ARROW key
Vk_select Selection Key
Vk_print Print key (keyboard specific)
Vk_execute Execution Key
Vk_snapshot Print Screen Key
Vk_insert Insertion Key
Vk_delete DELETE Key
Vk_help Help Key
Vk_lwin left Windows key (Microsoft Keyboard)
Vk_rwin right Windows key (Microsoft Keyboard)
Vk_apps Application key (Microsoft Keyboard)
Vk_numpad0 0 keys (numeric keypad)
Vk_numpad1 1 keys (numeric keypad)
Vk_numpad2 2 keys (numeric keypad)
Vk_numpad3 3 keys (numeric keypad)
VK_NUMPAD4 4 keys (numeric keypad)
VK_NUMPAD5 5 keys (numeric keypad)
VK_NUMPAD6 6 keys (numeric keypad)
Vk_numpad7 7 keys (numeric keypad)
Vk_numpad8 8 keys (numeric keypad)
VK_NUMPAD9 9 keys (numeric keypad)
Vk_multiply Multi-key (numeric keypad)
Vk_add add key (numeric keypad)
Vk_separator Separator key (numeric keypad)
Vk_subtract minus key (numeric keypad)
Vk_decimal decimal key (numeric keypad)
Vk_divide divide key (numeric keypad)
VK_F1 F1 Key
VK_F2 F2 Key
The key to Vk_f3 F3
Vk_f4 F4 Key
Vk_f5 F5 Key
Vk_f6 F6 Key
Vk_f7 F7 Key
Vk_f8 F8 Key
VK_F9 F9 Key
VK_F10 Key of F10 key
VK_F11 F11 Key
VK_F12 F12 Key
Vk_f13 F13 Key
Vk_f14 F14 Key
VK_F15 F15 Key
VK_F16 F16 Key
Vk_f17 F17 Key
Vk_f18 F18 Key
Vk_f19 F19 Key
Vk_f20 F20 Key
VK_F21 F21 Key
Vk_f22 F22 Key
Vk_f23 F23 Key
Vk_f24 F24 Key
Vk_numlock Number of lock keys
Vk_scroll SCROLL LOCK key
Vk_lshift left Shift key (for getasynckeystate and getkeystate only)
Vk_rshift Right SHIFT key (for getasynckeystate and getkeystate only)
Vk_lcontrol Left CTRL key (for Getasynckeystate and getkeystate only)
Vk_rcontrol Right CTRL key (for Getasynckeystate and getkeystate only)
Vk_lmenu left ALT key (for getasynckeystate and getkeystate only)
Vk_rmenu Right ALT key (for getasynckeystate and getkeystate only)
Vk_processkey Process Key
VK_ATTN Handling Key
Vk_crsel Crsel Key
Vk_exsel Exsel Key
Vk_ereof Erase EOF Analysis key
Vk_play Play the key
Vk_zoom Zoom Key
Vk_noname reserved for future use
VK_PA1 PA1 Key
Vk_oem_clear clear number of primary Tags: 1 "F14 key
VK_F15 F15 Key
VK_F16 F16 Key
Vk_f17 F17 Key
Vk_f18 F18 Key
Vk_f19 F19 Key
Vk_f20 F20 Key
VK_F21 F21 Key
Vk_f22 F22 Key
Vk_f23 F23 Key
Vk_f24 F24 Key
Vk_numlock Number of lock keys
Vk_scroll SCROLL LOCK key
Vk_lshift left Shift key (for getasynckeystate and getkeystate only)
Vk_rshift Right SHIFT key (for getasynckeystate and getkeystate only)
Vk_lcontrol Left CTRL key (for Getasynckeystate and getkeystate only)
Vk_rcontrol Right CTRL key (for Getasynckeystate and getkeystate only)
Vk_lmenu left ALT key (for getasynckeystate and getkeystate only)
Vk_rmenu Right ALT key (for getasynckeystate and getkeystate only)
Vk_processkey Process Key
VK_ATTN Handling Key
Vk_crsel Crsel Key
Vk_exsel Exsel Key
Vk_ereof Erase EOF Analysis key
Vk_play Play the key
Vk_zoom Zoom Key
Vk_noname reserved for future use
VK_PA1 PA1 Key
Vk_oem_clear Clear the main
Using Delphi to simulate keyboard input