Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Obtaining Input Form a Joystick with C# and DirectX Form A to Z

0.00/5 (No votes)
7 Dec 2014 2  
Taking you through a step by step process of programming joystick with C#

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.

  • Create a Windows Forms application named "RunJoyStickOnLocalMachine". Right click on your project in the solution explorer and select Add then select Existing Item, a select file window will appear to you, navigate to the file named Joystick.cs in the code attached with the article, and click on Ok button.
  • Right click on references button on the solution explorer window, then select Add Reference. From the top of the add reference window, select Browse tab and go to the path "C:\Windows\Microsoft.NET\DirectX for Managed Code\1.0.2902.0" and select "Microsoft.DirectX.DirectInput.dll" library.
  • Right click again on the project and select Add then select New Item from the list select Application Configuration File and click on Ok button. In the configuration file you just created, write the following tags, save and close the file:
    < startup useLegacyV2RuntimeActivationPolicy="true">
    < supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
    < /startup> 
    ?(Note that there is no space between the '<' and the tag, it should look like <startup ...>
  • You can now use the joystick class to create objects and start programming 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

Application Screen shot - maximum width is 600 pixels

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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here