C # Programming the game handle to develop-API (3)

Source: Internet
Author: User

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);
       }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.