A simple class that simulates keyboard and mouse operations. Expand the virtualkeys enumeration, or write it directly!
Using system;
Using system. runtime. interopservices;
Using system. text;
Class keyboard
{
Const uint keyeventf_extendedkey = 0x1;
Const uint keyeventf_keyup = 0x2;
[Dllimport ("user32.dll")]
Static extern short getkeystate (INT foreign rtkey );
[Dllimport ("user32.dll")]
Static extern void keybd_event (
Byte bvk,
Byte bscan,
Uint dwflags,
Uint dwextrainfo
);
Public Enum virtualkeys: byte
{
Vk_numlock = 0x90, // number lock key
Vk_scroll = 0x91, // scroll to lock
Vk_capital = 0x14, // case-sensitive lock
Vk_a = 62
}
Public static bool getstate (virtualkeys key)
{
Return (getkeystate (INT) Key) = 1 );
}
Public static void setstate (virtualkeys key, bool state)
{
If (State! = Getstate (key ))
{
Keybd_event (
(Byte) key,
0x45,
Keyeventf_extendedkey | 0,
0
);
Keybd_event (
(Byte) key,
0x45,
Keyeventf_extendedkey | keyeventf_keyup,
0
);
}
}
}
Example:
Simulated operation
Keyboard. setstate (
Virtualkeys. vk_capital,
! Keyboard. getstate (virtualkeys. vk_capital)
);
Obtain the keyboard status
Keyboard. getstate (virtualkeys. vk_capital)
* An IBM compatible keyboard with a decimal value identifier
--------------------------------------------------------------------------------
1 left vk_lbutton
2. Right-click vk_rbutton
3 vk_cancel Ctrl + break (usually not needed)
4. vk_mbutton
8 vk_back backspace
9 vk_tab
12 vk_clear num lock: specifies the number of keyboards when the lock is disabled.
13 vk_return enter (or another)
16 vk_shift shift (or another one)
17 vk_control CTRL (or another one)
18 vk_menu ALT (or another one)
19 vk_pause pause
20 vk_capital Caps Lock
27 vk_escape ESC
32 vk_space spacebar
33 vk_prior page up
34 vk_next page down
35 vk_end end
36 vk_home home
37 vk_left left arrow
38. Up Arrow of vk_up
39 vk_right right arrow
40 vk_down down arrow
41 vk_select (optional)
42 vk_print (optional)
43 vk_execute (optional)
44 vk_snapshot Print Screen
45 vk_insert insert
46 vk_delete Delete
47 vk_help (optional)
48 ~ 57. 0 ~ 9
65 ~ 90 no ~ Z
96 ~ 105 vk_numpad0 ~ When vk_numpad9 num lock is enabled, the value 0 ~ 9
106 vk_nultiply *
107 vk_add + on the numeric keyboard
108 vk_separator optional
109 vk_subtract-
110 on the vk_decimal numeric keyboard.
111 vk_divide/
112 ~ 135 vk_f1 ~ Vk_f24 function key F1 ~ F24
144 vk_numlock Num Lock
145 vk_scroll scroll lock
*/
I suddenly found that there was another one in C #.System. Windows. Forms. sendkeys
However, this can only simulate a keyboard.
Here is the code that can really simulate mouse operations! I am looking for something so hard!
Function declaration:
Private readonly int mouseeventf_leftdown = 0x2;
Private readonly int mouseeventf_leftup = 0x4;
[Dllimport ("USER32")]
Public static extern void mouse_event (INT dwflags, int dx, int dy, int dwdata, int dwextrainfo );
Call method:
Mouse_event (mouseeventf_leftdown, x * 65536/1024, y * 65536/768, 0, 0 );
Mouse_event (mouseeventf_leftup, x 65536/1024, y * 65536/768, 0, 0 );
X and Y are the abscissa and ordinate of the point you want to click.