C # programming and development of game controllers-API (1)

Source: Internet
Author: User

Some time ago, I spent 38 RMB to buy a USB game handle from the Internet, so that I can play SFC and arcade simulation games for my children on weekends and evenings.

 

When playing a Flash game on a website one day, I suddenly thought that it would be nice if I could also use a controller to play the Flash game. But unfortunately, Flash currently does not support programming the game controller, which is a pity in Flash ..

Although Flash does not support programming the game controller, we can use another method to create an auxiliary program (plug-in? ), Convert the operation event in the handle to an acceptable keyboard and mouse operation event in Flash, so that you can use the game handle to play the Flash game ?! As a result, I checked the relevant information online, but found that only the C ++ case was found, and C # could not find one. This was not a hard nut to crack. I had to do it myself to improve my health.

 

 

(Note: similar to this function, there is already a ready-made software on the network, which is developed by a Japanese called JoyToKey)

 

There are two ways to operate the gamepad: using system APIs or using DirectInput. (There may be other methods, but my knowledge scope is limited, and other methods are unknown)

Using System APIS is the simplest way, because the system has helped us encapsulate all the details, we only need to regularly obtain the status of the game controller device in the Program (Round Robin ).

 

The following APIs are used to operate game controllers (rods )::

Function Name Function Description
JoyGetNumDevs Obtain the number of game devices supported by the current system
JoyGetDevCaps Query and obtain a specified gamer device to determine its performance
JoySetCapture Apply to the system to capture a game device and regularly send the status value of the device to a window through a message
JoyReleaseCapture Release the capture of a game device
JoyGetPos Obtains the coordinates and buttons of a game device.
JoyGetPosEx Obtains the coordinates and buttons of a game device.
JoyGetThreshold Queries the current moving threshold of a specified gamer's device.
JoySetThreshold Sets the threshold for moving a specified gamer device.

 

There are two ways to call different methods.

1) passive mode:

Call the joySetCapture method to apply to the system for capturing a game handle. If the application is successful, the system willTimingThe status information of the gamepad is sent to a window.

2) Active Mode:

That is, according to our own needs,On DemandCall the joyGetPos or joyGetPosEx method to queryCurrentStatus.

 

In this article, we will only explain "passive mode ".

 

JoySetCaptureThe C # definition prototype of the method is as follows:

/// <Summary>
/// Apply to the system to capture a game rod and regularly send the status value of the device to a window through the message
/// </Summary>
/// <Param name = "hWnd"> window handle </param>
/// <Param name = "uJoyID"> specify the player lever (0-15), which can be JOYSTICKID1 or JOYSTICKID2 </param>
/// <Param name = "uPeriod"> sends information about the game pole to the application at a given polling interval. This parameter is the unit of polling frequency. </Param>
/// <Param name = "fChanged"> whether to allow the program to receive messages when the joystick moves a certain distance </param>
/// <Returns> </returns>
[DllImport ("winmm. dll")]
Public static extern int joySetCapture (IntPtr hWnd, int uJoyID, int uPeriod, bool fChanged );

When we call this method to apply to the system for capturing a game handle, if it succeeds, JOYERR_NOERROR (value: 0) is returned; otherwise, the application fails if other values are returned. You must call the joyReleaseCapture method to release the capture when you no longer need to capture the gamepad.

 

If the application is successful, the system periodically sends the status of the game controller to the corresponding window interface of hWnd in the form of a message package (based on the uPeriod value to determine the length of time. Therefore, we must process the corresponding messages in the Program (for example, rewrite the WndProc method for processing ).

The system sends different message numbers based on different uJoyID values. For example, for JOYSTICKID1, the system sends the following message packets respectively:

Message number Description
MM_JOY1MOVE This message package is sent when the location of the handle has changed or when some buttons are pressed.
MM_JOY1BUTTONDOWN When one or more buttons A, B, C, and D are being pressed, the message package is sent.
MM_JOY1BUTTONUP When one or more buttons A, B, C, and D of the handle are being popped up, the message package is sent.

 

The message packages sent by the JOYSTICKID2 system are MM_JOY2MOVE, MM_JOY2BUTTONDOWN, and MM_JOY2BUTTONUP!

AndNote:! Whether you press the button on the gamepad or not, the system periodically sends the message MM_JOYXMOVE !!

 

How to determine which keys are pressed?

In the message package, the status information (Button status) of the game controller is stored in the WParam and LParam parameters of the message package respectively.

1) WParam parameters:

For game controllers, WParam storesDivisionFourDirection keyThe value of the currently pressed Button in all other buttons. Its value is a compound value. For example, if its value is JOY_BUTTON1 | JOY_BUTTON2, it indicates that the keys pressed are buttons 1 and 2.

Note: For MM_JOYXBUTTONDOWN and MM_JOYXBUTTONUP messages, the button value used for judging is different from the button value of MM_JOYXMOVE !!

 

2) LParam parameters:

This parameter stores the coordinates of the game controller, and the 16-bit height stores the Y coordinate values and the 16-bit lower stores the X coordinate values.

For the gamepad, this parameter is used to determine whether or not the four arrow keys are pressed. If neither of the four direction keys is pressed, it indicates that the current game controller is in the central coordinate! That is, the coordinates of X and Y are all at the center point. When some direction keys are pressed, the coordinates of X and Y are shifted to the corresponding direction based on the key. If you press the right-click button, the X coordinates are shifted to the right, and the Y coordinates are kept at the center. If you press both the right and top direction keys at the same time, the X coordinates are shifted to the right, Y coordinates are offset up. Therefore, we can obtain the value of X and Y coordinates based on the LParam parameter, and then judge it based on its center point. The reference code is as follows:

/// <Summary>
/// Obtain the status of X and Y axes
/// </Summary>
/// <Param name = "lParam"> </param>
/// <Param name = "buttons"> </param>
Private void GetXYButtonsStateFromLParam (int lParam, ref JoystickButtons buttons)
{
// Process X and Y axes
Int x = lParam & 0x0000FFFF; // low 16-bit storage x axis coordinates
Int y = (int) (lParam & 0xFFFF0000)> 16); // The High 16-bit y axis is stored (not directly displaced to avoid 0xffffffff)
Int m = 0x7EFF; // center point value
If (x> m)
{
Buttons | = JoystickButtons. Right;
}
Else if (x <m)
{
Buttons | = JoystickButtons. Left;
}
If (y> m)
{
Buttons | = JoystickButtons. Down;
}
Else if (y <m)
{
Buttons | = JoystickButtons. UP;
}
}

 

Now, the "passive mode" programming of the game controller is complete, and the rest is how to use the game controller to simulate keyboard or mouse operations ......

PS: If you are interested, consider how to implement "active" programming and development of game controllers.

 

Download sample code:

/Files/kingthy/JoyKeys.rar

Related Article

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.