Review "passive mode" development
In C # for game handle Programming development-API (1) In this article we introduced the "passive mode" of development. In this way, our program only acts as a message recipient. The system will regularly tell us the current state of a game handle, our program received after the actual need for processing can be. But if you are a careful person, you will find that if you deal directly with the message event, there is a problem, if we press a key (such as the UP ARROW key) and then let go, for our "people", we press and bounce the two actions should only show that we click on this button once. But for the system, it is only mechanically timed to notify us of the state of the various buttons of the game handle at a certain time, and in the time we press to bounce, it is possible that the system has passed the N-time message notification (n values are determined according to the Uperiod value set at the time of capture and the speed of your keystrokes), The notification handle has a button in the pressed state, and if we deal directly with the Click event based on the message packet, it can cause problems (for example, in a game, we design the right key to the handle once and then move the character one step forward.) But from the time we pressed the button to the flick of the button, because people reacted much slower than the speed of the computer, so this is a very short period of time, the system may have been notified more than 10 message packets indicate that the game handle right button has been pressed, which causes us to press the right button, the role in the game may have moved more than 10 steps , this is not the result we want. So how do we handle this "click" event to avoid duplicate notifications? This is the end of this article to focus on the content of ...
Before we explain the solution to this problem, let's talk about one of the development methods mentioned above.
Development of "active mode"
The active way that we do not need to register to the system to capture a game handle, we just according to their own needs on time to get the game handle status information.
At this point we will use the following API functions.
/// <summary>
/// 获取操纵杆位置和按钮状态
/// </summary>
/// <param name="uJoyID"></param>
/// <param name="pji"></param>
/// <returns></returns>
[DllImport("winmm.dll")]
public static extern int joyGetPos(int uJoyID, ref JOYINFO pji);
/// <summary>
/// 获取操纵杆位置和按钮状态
/// </summary>
/// <param name="uJoyID"></param>
/// <param name="pji"></param>
/// <returns></returns>
[DllImport("winmm.dll")]
public static extern int joyGetPosEx(int uJoyID, ref JOYINFOEX pji);