标签:style blog http io color ar os for sp
对系统模拟按键方面的知识和按键映射代码做了一下梳理,在这里分享出来,适用于开发自动操作工具和游戏外挂。
主代码:
public const int KEYEVENTF_EXTENDEDKEY = 0x0001; //Key click flag
public const int KEYEVENTF_KEYUP = 0x0002; //Key up flag
[DllImport("user32.dll")]
private static extern void keybd_event(byte bVk, byte bSCan, int dwFlags, int dwExtraInfo);
[DllImport("user32.dll")]
private static extern byte MapVirtualKey(byte wCode, int wMap);
public static void 模拟按下按键(VirtualKeyCode 虚拟按键代码)
{
var code = (byte)虚拟按键代码;
keybd_event(code, 0, 0, 0);
}
public static void 模拟弹起按键(VirtualKeyCode 虚拟按键代码)
{
var code = (byte) 虚拟按键代码;
keybd_event(code, 0, KEYEVENTF_KEYUP, 0);
}
public static void 模拟单击按键(VirtualKeyCode 虚拟按键代码)
{
var code = (byte)虚拟按键代码;
keybd_event(code, 0, KEYEVENTF_EXTENDEDKEY, 0);
}
网上关于keybd_event的dwFlags参数功能说法很混乱,经我测试貌似是KEYEVENTF_EXTENDEDKEY表示一次单击,0表示按下,KEYEVENTF_KEYUP表示弹起,不一定完全正确,希望高人指点一下。
另外MapVirtualKey的作用实在不懂,所以就没用上,看到有人这么调用不知有什么区别:
var code = (byte)虚拟按键代码;
keybd_event(code, MapVirtualKey(code,0), 0, 0);
我试过好像也没什么变化~到底MapVirtualKey是干什么用的呢??
VirtualKeyCode枚举:
///
/// 虚拟按键代码
/// 参考于 http://msdn.microsoft.com/zh-cn/library/dd375731(v=vs.85).aspx
///
public enum VirtualKeyCode
{
///
/// Left mouse button
///
Left_mouse_button = 0x01,
///
/// Right mouse button
///
Right_mouse_button = 0x02,
///
/// Control-break processing
///
Control_break_processing = 0x03,
///
/// Middle mouse button (three-button mouse)
///
Middle_mouse_button = 0x04,
///
/// X1 mouse button
///
X1_mouse_button = 0x05,
///
/// X2 mouse button
///
X2_mouse_button = 0x06,
///
/// Undefined
///
Undefined1 = 0x07,
///
/// BACKSPACE key
///
BACKSPACE_key = 0x08,
///
/// TAB key
///
TAB_key = 0x09,
///
/// CLEAR key
///
CLEAR_key = 0x0C,
///
/// ENTER key
///
ENTER_key = 0x0D,
///
/// SHIFT key
///
SHIFT_key = 0x10,
///
/// CTRL key
///
CTRL_key = 0x11,
///
/// ALT key
///
ALT_key = 0x12,
///
/// PAUSE key
///
PAUSE_key = 0x13,
///
/// CAPS LOCK key
///
CAPS_LOCK_key = 0x14,
///
/// IME Kana mode
///
IME_Kana_mode = 0x15,
///
/// IME Hanguel mode (maintained for compatibility; use VK_HANGUL)
///
IME_Hanguel_mode = 0x15,
///
/// IME Hangul mode
///
IME_Hangul_mode = 0x15,
///
/// Undefined
///
Undefined2 = 0x16,
///
/// IME Junja mode
///
IME_Junja_mode = 0x17,
///
/// IME final mode
///
IME_final_mode = 0x18,
///
/// IME Hanja mode
///
IME_Hanja_mode = 0x19,
///
/// IME Kanji mode
///
IME_Kanji_mode = 0x19,
///
/// Undefined
///
Undefined = 0x1A,
///
/// ESC key
///
ESC_key = 0x1B,
///
/// IME convert
///
IME_convert = 0x1C,
///
/// IME nonconvert
///
IME_nonconvert = 0x1D,
///
/// IME accept
///
IME_accept = 0x1E,
///
/// IME mode change request
///
IME_mode_change_request = 0x1F,
///
/// SPACEBAR
///
SPACEBAR = 0x20,
///
/// PAGE UP key
///
PAGE_UP_key = 0x21,
///
/// PAGE DOWN key
///
PAGE_DOWN_key = 0x22,
///
/// END key
///
END_key = 0x23,
///
/// HOME key
///
HOME_key = 0x24,
///
/// LEFT ARROW key
///
LEFT_ARROW_key = 0x25,
///
/// UP ARROW key
///
UP_ARROW_key = 0x26,
///
/// RIGHT ARROW key
///
RIGHT_ARROW_key = 0x27,
///
/// DOWN ARROW key
///
DOWN_ARROW_key = 0x28,
///
/// SELECT key
///
SELECT_key = 0x29,
///
/// PRINT key
///
PRINT_key = 0x2A,
///
/// EXECUTE key
///
EXECUTE_key = 0x2B,
///
/// PRINT SCREEN key
///
PRINT_SCREEN_key = 0x2C,
///
/// INS key
///
INS_key = 0x2D,
///
/// DEL key
///
DEL_key = 0x2E,
///
/// HELP key
///
HELP_key = 0x2F,
///
/// 0 key
///
_0_key = 0x30,
///
/// 1 key
///
_1_key = 0x31,
///
/// 2 key
///
_2_key = 0x32,
///
/// 3 key
///
_3_key = 0x33,
///
/// 4 key
///
_4_key = 0x34,
///
/// 5 key
///
_5_key = 0x35,
///
/// 6 key
///
_6_key = 0x36,
///
/// 7 key
///
_7_key = 0x37,
///
/// 8 key
///
_8_key = 0x38,
///
/// 9 key
///
_9_key = 0x39,
///
/// A key
///
A_key = 0x41,
///
/// B key
///
B_key = 0x42,
///
/// C key
///
C_key = 0x43,
///
/// D key
///
D_key = 0x44,
///
/// E key
///
E_key = 0x45,
///
/// F key
///
F_key = 0x46,
///
/// G key
///
G_key = 0x47,
///
/// H key
///
H_key = 0x48,
///
/// I key
///
I_key = 0x49,
///
/// J key
///
J_key = 0x4A,
///
/// K key
///
K_key = 0x4B,
///
/// L key
///
L_key = 0x4C,
///
/// M key
///
M_key = 0x4D,
///
/// N key
///
N_key = 0x4E,
///
/// O key
///
O_key = 0x4F,
///
/// P key
///
P_key = 0x50,
///
/// Q key
///
Q_key = 0x51,
///
/// R key
///
R_key = 0x52,
///
/// S key
///
S_key = 0x53,
///
/// T key
///
T_key = 0x54,
///
/// U key
///
U_key = 0x55,
///
/// V key
///
V_key = 0x56,
///
/// W key
///
W_key = 0x57,
///
/// X key
///
X_key = 0x58,
///
/// Y key
///
Y_key = 0x59,
///
/// Z key
///
Z_key = 0x5A,
///
/// Left Windows key (Natural keyboard)
///
Left_Windows_key = 0x5B,
///
/// Right Windows key (Natural keyboard)
///
Right_Windows_key = 0x5C,
///
/// Applications key (Natural keyboard)
///
Applications_key = 0x5D,
///
/// Reserved
///
Reserved1 = 0x5E,
///
/// Computer Sleep key
///
Computer_Sleep_key = 0x5F,
///
/// Numeric keypad 0 key
///
Numeric_keypad_0_key = 0x60,
///
/// Numeric keypad 1 key
///
Numeric_keypad_1_key = 0x61,
///
/// Numeric keypad 2 key
///
Numeric_keypad_2_key = 0x62,
///
/// Numeric keypad 3 key
///
Numeric_keypad_3_key = 0x63,
///
/// Numeric keypad 4 key
///
Numeric_keypad_4_key = 0x64,
///
/// Numeric keypad 5 key
///
Numeric_keypad_5_key = 0x65,
///
/// Numeric keypad 6 key
///
Numeric_keypad_6_key = 0x66,
///
/// Numeric keypad 7 key
///
Numeric_keypad_7_key = 0x67,
///
/// Numeric keypad 8 key
///
Numeric_keypad_8_key = 0x68,
///
/// Numeric keypad 9 key
///
Numeric_keypad_9_key = 0x69,
///
/// Multiply key
///
Multiply_key = 0x6A,
///
/// Add key
///
Add_key = 0x6B,
///
/// Separator key
///
Separator_key = 0x6C,
///
/// Subtract key
///
Subtract_key = 0x6D,
///
/// Decimal key
///
Decimal_key = 0x6E,
///
/// Divide key
///
Divide_key = 0x6F,
///
/// F1 key
///
F1_key = 0x70,
///
/// F2 key
///
F2_key = 0x71,
///
/// F3 key
///
F3_key = 0x72,
///
/// F4 key
///
F4_key = 0x73,
///
/// F5 key
///
F5_key = 0x74,
///
/// F6 key
///
F6_key = 0x75,
///
/// F7 key
///
F7_key = 0x76,
///
/// F8 key
///
F8_key = 0x77,
///
/// F9 key
///
F9_key = 0x78,
///
/// F10 key
///
F10_key = 0x79,
///
/// F11 key
///
F11_key = 0x7A,
///
/// F12 key
///
F12_key = 0x7B,
///
/// F13 key
///
F13_key = 0x7C,
///
/// F14 key
///
F14_key = 0x7D,
///
/// F15 key
///
F15_key = 0x7E,
///
/// F16 key
///
F16_key = 0x7F,
///
/// F17 key
///
F17_key = 0x80,
///
/// F18 key
///
F18_key = 0x81,
///
/// F19 key
///
F19_key = 0x82,
///
/// F20 key
///
F20_key = 0x83,
///
/// F21 key
///
F21_key = 0x84,
///
/// F22 key
///
F22_key = 0x85,
///
/// F23 key
///
F23_key = 0x86,
///
/// F24 key
///
F24_key = 0x87,
///
/// NUM LOCK key
///
NUM_LOCK_key = 0x90,
///
/// SCROLL LOCK key
///
SCROLL_LOCK_key = 0x91,
///
/// Left SHIFT key
///
Left_SHIFT_key = 0xA0,
///
/// Right SHIFT key
///
Right_SHIFT_key = 0xA1,
///
/// Left CONTROL key
///
Left_CONTROL_key = 0xA2,
///
/// Right CONTROL key
///
Right_CONTROL_key = 0xA3,
///
/// Left MENU key
///
Left_MENU_key = 0xA4,
///
/// Right MENU key
///
Right_MENU_key = 0xA5,
///
/// Browser Back key
///
Browser_Back_key = 0xA6,
///
/// Browser Forward key
///
Browser_Forward_key = 0xA7,
///
/// Browser Refresh key
///
Browser_Refresh_key = 0xA8,
///
/// Browser Stop key
///
Browser_Stop_key = 0xA9,
///
/// Browser Search key
///
Browser_Search_key = 0xAA,
///
/// Browser Favorites key
///
Browser_Favorites_key = 0xAB,
///
/// Browser Start and Home key
///
Browser_Start_and_Home_key = 0xAC,
///
/// Volume Mute key
///
Volume_Mute_key = 0xAD,
///
/// Volume Down key
///
Volume_Down_key = 0xAE,
///
/// Volume Up key
///
Volume_Up_key = 0xAF,
///
/// Next Track key
///
Next_Track_key = 0xB0,
///
/// Previous Track key
///
Previous_Track_key = 0xB1,
///
/// Stop Media key
///
Stop_Media_key = 0xB2,
///
/// Play/Pause Media key
///
Play_Or_Pause_Media_key = 0xB3,
///
/// Start Mail key
///
Start_Mail_key = 0xB4,
///
/// Select Media key
///
Select_Media_key = 0xB5,
///
/// Start Application 1 key
///
Start_Application_1_key = 0xB6,
///
/// Start Application 2 key
///
Start_Application_2_key = 0xB7,
///
/// Used for miscellaneous characters; it can vary by keyboard.
///
Used_for_miscellaneous_characters1 = 0xBA,
///
/// Used for miscellaneous characters; it can vary by keyboard.
///
Used_for_miscellaneous_characters2 = 0xBF,
///
/// Used for miscellaneous characters; it can vary by keyboard.
///
Used_for_miscellaneous_characters3 = 0xC0,
///
/// Used for miscellaneous characters; it can vary by keyboard.
///
Used_for_miscellaneous_characters4 = 0xDB,
///
/// Used for miscellaneous characters; it can vary by keyboard.
///
Used_for_miscellaneous_characters5 = 0xDC,
///
/// Used for miscellaneous characters; it can vary by keyboard.
///
Used_for_miscellaneous_characters6 = 0xDD,
///
/// Used for miscellaneous characters; it can vary by keyboard.
///
Used_for_miscellaneous_characters7 = 0xDE,
///
/// Used for miscellaneous characters; it can vary by keyboard.
///
Used_for_miscellaneous_characters8 = 0xDF,
///
/// Reserved
///
Reserved2 = 0xE0,
///
/// OEM specific
///
OEM_specific1 = 0xE1,
///
/// Either the angle bracket key or the backslash key on the RT 102-key keyboard
///
Either_the_angle_bracket_key_or_the_backslash_key_on_the_RT_102_key_keyboard = 0xE2,
///
/// IME PROCESS key
///
IME_PROCESS_key = 0xE5,
///
/// OEM specific
///
OEM_specific2 = 0xE6,
///
/// Used to pass Unicode characters as if they were keystrokes. The VK_PACKET key is the low word of a 32-bit Virtual Key value used for non-keyboard input methods. For more information, see Remark in KEYBDINPUT, SendInput, WM_KEYDOWN, and WM_KEYUP
///
Used_to_pass_Unicode_characters_as_if_they_were_keystrokes = 0xE7,
///
/// Unassigned
///
Unassigned = 0xE8,
///
/// Attn key
///
Attn_key = 0xF6,
///
/// CrSel key
///
CrSel_key = 0xF7,
///
/// ExSel key
///
ExSel_key = 0xF8,
///
/// Erase EOF key
///
Erase_EOF_key = 0xF9,
///
/// Play key
///
Play_key = 0xFA,
///
/// Zoom key
///
Zoom_key = 0xFB,
///
/// Reserved
///
Reserved = 0xFC,
///
/// PA1 key
///
PA1_key = 0xFD,
///
/// Clear key
///
Clear_key = 0xFE
}
调用演示:
//模拟实现Ctrl+O操作
模拟按下按键(VirtualKeyCode.CTRL_key)
模拟单击按键(VirtualKeyCode.O_key)
模拟弹起按键(VirtualKeyCode.CTRL_key)
整理分享C#通过user32.dll模拟物理按键操作的代码
标签:style blog http io color ar os for sp
原文地址:http://www.cnblogs.com/SkyD/p/4070743.html