Click here to Skip to main content
15,034,432 members
Articles / Web Development / HTML
Posted 7 Dec 2014


12 bookmarked

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

Rate me:
Please Sign up or sign in to vote.
4.40/5 (8 votes)
7 Dec 2014CPOL3 min read
Taking you through a step by step process of programming joystick with C#

Image 1


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.


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)
        joystickButtons = joystick.buttons;
        if (joystick.Xaxis == 0)
        if (joystick.Xaxis == 65535)
        if (joystick.Yaxis == 0)
        if (joystick.Yaxis == 65535)
        for (int i = 0; i < joystickButtons.Length; i++)
            if(joystickButtons[i] == true)
                output.Text+="Button " + i + " Pressed\n";
        joystickTimer.Enabled = false;

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


  • Friday, 6th December, 2014 - First version


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


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

Comments and Discussions

QuestionDoes it work with .Net Core? Pin
Rui Monteiro 202115-Sep-21 9:42
MemberRui Monteiro 202115-Sep-21 9:42 
GeneralMy vote of 3 Pin
Ian Ilo5-Jun-19 6:03
MemberIan Ilo5-Jun-19 6:03 
GeneralMy vote of 5 Pin
Ian Ilo2-Jun-19 17:01
MemberIan Ilo2-Jun-19 17:01 
GeneralMy vote of 4 Pin
Ian Ilo2-Jun-19 0:03
MemberIan Ilo2-Jun-19 0:03 
Questionunable to load program if no joystick is connected, PLEASE HELP Pin
Johnny Fonseca4-Nov-18 5:17
MemberJohnny Fonseca4-Nov-18 5:17 
AnswerRe: unable to load program if no joystick is connected, PLEASE HELP Pin
Johnny Fonseca4-Nov-18 9:29
MemberJohnny Fonseca4-Nov-18 9:29 
Questionhow to load Dll into visual studio 2015 or 2017 and Unity3d Pin
hh869-Mar-18 0:03
Memberhh869-Mar-18 0:03 
QuestionWorks well! Pin
jshafer81713-Jan-18 18:41
Memberjshafer81713-Jan-18 18:41 
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.