Introduction
Over few days, I was searching the web for a complete wizard or tutorial for how to operate joystick with C#. Unfortunately, I didn't find any step by step tutorial to help me control my robot "iRobot create" through a joystick. After integrating more than one class (most of them written by Eng. Ebram K William ) and library, I decided to write my own tutorial and publish it in CodeProject to be available for others. In this tutorial, we will learn how to create your own application using a little of C# and a general purpose joystick. Throughout the tutorial, we will provide you with the little information you need to know to use a Joystick in your application, but for interested readers, you can check the last section for further references.
DirectX
In this tutorial, we will make use of Microsoft DirectX
. DirectX
is a platform that provides you with a set of namespaces and classes (API) that help you in developing multimedia applications. DirectX
contains classes to work with 2D and 3D drawings, other namespaces deal with audio, music and input devices. DirectX
contains more than API such as:
Direct3D
Graphics
DirectDraw
DirectInput
DirectPlay
DirectSound
- Audio Video Playback
In our tutorial, we deal with DirectInput
component in DirectX
API, which is responsible for getting input from various input devices such as keyboard, mouse, joystick and other game controllers.
Using the Code
In this tutorial, we used Visual Studio 2010 with DirectX
installed in the system. In your system, you can download the DirectX
SDK from the following link Download DirectX SDK from the official site. After installing the DirectX
SDK, you are ready to go with creating your application.
If you are not interested in creating your own application, just follow the previous steps to run the attached project (Application) and instead of writing on the output text field, insert the code you need to run on clicking this button. The code segment you should modify is:
private void joystickTimer_Tick_1(object sender, EventArgs e)
{
try
{
joystick.UpdateStatus();
joystickButtons = joystick.buttons;
if (joystick.Xaxis == 0)
output.Text+="Left\n";
if (joystick.Xaxis == 65535)
output.Text+="Right\n";
if (joystick.Yaxis == 0)
output.Text+="Up\n";
if (joystick.Yaxis == 65535)
output.Text+="Down\n";
for (int i = 0; i < joystickButtons.Length; i++)
{
if(joystickButtons[i] == true)
output.Text+="Button " + i + " Pressed\n";
}
}
catch
{
joystickTimer.Enabled = false;
connectToJoystick(joystick);
}
}
Obtaining Input
A general purpose joystick has 10 buttons (The order of the buttons as illustrated in the above demo picture) which are returned in an array named buttons in the joystick
object, each element in the array contains a boolean value indicating whether this button is pressed or no. It also contains four arrow keys, the status of those arrows are controlled through two int
attributes, Xaxis represents the left and right properties and Yaxis represents the up and down arrows. If the value of the Xaxis is (zero) then the left arrow is pressed, if it equals to (65535), then the right arrow is pressed. Also in Yaxis, it equals (zero) for up or (65535) for down.
Note and Thanks
History
- Friday, 6th December, 2014 - First version