Click here to Skip to main content
13,192,207 members (32,697 online)
Click here to Skip to main content
Add your own
alternative version

Stats

19.7K views
3.4K downloads
9 bookmarked
Posted 7 Dec 2014

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

, 7 Dec 2014
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Mostafa Korashy
Instructor / Trainer Faculty of Computers and Information
Egypt Egypt
A demonstrator at faculty of computers and information (FCI), Assiut university. Interesting in programming, robotics, and vision. You can follow my recent researches and academic progress at

https://www.researchgate.net/profile/Mostafa_Korashy

You may also be interested in...

Comments and Discussions

 
QuestionAccess 2nd stick of Xbox 360 controller? Pin
Member 134408932-Oct-17 4:04
memberMember 134408932-Oct-17 4:04 
PraiseWorks Well Pin
bluesky-software16-Sep-17 0:51
memberbluesky-software16-Sep-17 0:51 
Questionproblem Pin
Member 1181476229-Jan-16 23:53
memberMember 1181476229-Jan-16 23:53 
AnswerRe: problem Pin
MadeByVince22-Nov-16 21:30
memberMadeByVince22-Nov-16 21:30 
GeneralRe: problem Pin
-tusk-12-Jun-17 11:52
member-tusk-12-Jun-17 11:52 
GeneralMy vote of 5 Pin
Ahmed Hamdy17-Jul-15 3:40
memberAhmed Hamdy17-Jul-15 3:40 
GeneralRe: My vote of 5 Pin
Mostafa Korashy3-Apr-16 0:09
professionalMostafa Korashy3-Apr-16 0:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171017.2 | Last Updated 7 Dec 2014
Article Copyright 2014 by Mostafa Korashy
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid