|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionWhen trying to find some code to use my joystick through C#, I found a sad lack of articles. There were a few posts on forums with bits and pieces of code, but no solid code on how you can use it. The scenario of a 6-axis, 20+ buttoned joystick does not seem to have occured in any C# articles before - much less on CodeProject. I intend to demonstrate how to acquire and use a full 6-axis joystick, plus all the buttons availiable. Managed DirectXAs Managed DirectX (MDX) 2.0 is not yet released and will be included in XNA, this article uses Managed DirectX 1.1, which is fully compatible with .NET 2.0. However, you will need to change a setting within Visual Studio 2005 or a Loader Lock exception will be thrown as soon as you attempt to get a device list. The setting you need to change in VS 2005 is under the Debug menu, in Exceptions. In Managed Debugging Asssitants, uncheck "Thrown" on Loader Lock. This will allow you to debug your application. Once you have downloaded and installed the SDK, you can add a reference to Finding Your DeviceTo locate a joystick, a list of all game controllers that are attached to the system needs to be obtained; the The variable // Find all the GameControl devices that are attached.
DeviceList gameControllerList = Manager.GetDevices(DeviceClass.GameControl,
EnumDevicesFlags.AttachedOnly);
// check that we have at least one device.
if (gameControllerList.Count > 0)
{
// Move to the first device
gameControllerList.MoveNext();
DeviceInstance deviceInstance = (DeviceInstance)
gameControllerList.Current;
// create a device from this controller.
joystickDevice = new Device(deviceInstance.InstanceGuid);
joystickDevice.SetCooperativeLevel(this,
CooperativeLevelFlags.Background |
CooperativeLevelFlags.NonExclusive);
}
// Tell DirectX that this is a Joystick.
joystickDevice.SetDataFormat(DeviceDataFormat.Joystick);
// Finally, acquire the device.
joystickDevice.Acquire();
The What is the Joystick Capable of?This device can now be queried to find out what it is capable of. The // Find the capabilities of the joystick
DeviceCaps cps = joystickDevice.Caps;
// number of Axes
Debug.WriteLine("Joystick Axis: " + cps.NumberAxes);
// number of Buttons
Debug.WriteLine("Joystick Buttons: " + cps.NumberButtons);
// number of PoV hats
Debug.WriteLine("Joystick PoV hats: " + cps.NumberPointOfViews);
These numbers will probably play an important role when writing an application implementing the joystick. Getting Device InputEach time the current state of the joystick is required, the private void Poll ( )
{
try
{
// poll the joystick
joystickDevice.Poll();
// update the joystick state field
state = joystickDevice.CurrentJoystickState;
}
catch (Exception err)
{
// we probably lost connection to the joystick
// was it unplugged or locked by another application?
Debug.WriteLine(err.Message);
}
}
The exception should probably be dealt with a little smarter, but at the moment, it doesn't matter. This method will need to be called each time we want to retrieve anything from the joystick. Once the device has been polled for its current state, the axes positions and button state can be retrieved from the int axisA = state.Rz;
int axisB = state.Rx;
int axisC = state.X;
int axisD = state.Y;
The bool buttonA = buttons[0] >= 128;
You can do this for each of your joystick's buttons. Additionally, PoV hats also show up in the Finding Additional InputsIf the joystick has additional axes that are not availiable as properties on the int[] extraAxis = state.GetSlider();
int axisE = extraAxis[0];
int axisF = extraAxis[1];
ConclusionHopefully, this information will be useful to you and you'll be able to use your joystick without any issues. This article only covers the basics and you may wish to implement Force Feedback or PoV hats or other technologies that are availiable. History
|
||||||||||||||||||||||