The game handles the programming development in two, has explained, in this article will explain to the keyboard simulation.
The simulation of the keyboard, the system has provided a lot of API functions, here is not one to explain, only the simplest of the API functions.
/// <summary>
/// 模拟键盘事件
/// </summary>
/// <param name="bVk">虚拟键值</param>
/// <param name="bScan"></param>
/// <param name="dwFlags"></param>
/// <param name="dwExtraInfo">附加键状</param>
[DllImport("user32.dll")]
public static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo);
Where BVK can be directly corresponding to the value of System.Windows.Forms.Keys; dwflags can be a combination of the following values:
Constant |
Description |
Keyeventf_keydown |
KeyDown events |
Keyeventf_extendedkey |
Indicates that the bscan is an extension key code. |
Keyeventf_keyup |
KeyUp events |
To facilitate further encapsulation of this API:
/// <summary>
/// 键盘事件
/// </summary>
[Flags]
public enum KeyboardEvents
{
None = 0x0,
KeyDown = 0x1,
KeyUp = 0x2
}
/// <summary>
/// 发送键盘事件
/// </summary>
/// <param name="key"></param>
/// <param name="events">事件</param>
public static void SendKeyEvent(Keys key, KeyboardEvents events)
{
if ((events & KeyboardEvents.KeyDown) == KeyboardEvents.KeyDown)
keybd_event((byte)key, 0, KEYEVENTF_KEYDOWN, 0);
if ((events & KeyboardEvents.KeyUp) == KeyboardEvents.KeyUp)
keybd_event((byte)key, 0, KEYEVENTF_KEYUP, 0);
}