Click here to Skip to main content
15,881,852 members
Articles / Desktop Programming / Win32
Tip/Trick

XBOX 360 Controller Class

Rate me:
Please Sign up or sign in to vote.
3.67/5 (2 votes)
30 Dec 2014CPOL 23K   19   16   4
A class representing the state of a connected XBOX 360 controller

Introduction

Well, this class is pretty much the result of my attempt to write an alternative to the Xpadder application by Jonathan (don't know the last name). But because I decided to use C# to realize the application, I had to find a way to catch the input from the controller and because .NET doesn't provide an interface for that, I had to create my own way.

Requirements

Other than .NET 4.x

Using the Code

Other than usual, I'll provide a quick overview. But the source code should provide more than enough hints and if not, the comment section is below this tip.

Creating a New GamepadState Instance

C#
using System;
using SlimDX;
using SlimDX.XInput;
using SlimDXPlugin.XInput;

namespace MyApplication {

  public class MyApplication {

    //...

    public GamepadState Controller1 = new GamepadState(UserIndex.One);
    public GamepadState Controller2 = new GamepadState(UserIndex.Two);

  }
}

Binding Events

C#
using System;
using SlimDX;
using SlimDX.XInput;
using SlimDXPlugin.XInput;

namespace MyApplication {

  public class MyApplication {

    //...

    public GamepadState Controller1 = new GamepadState(UserIndex.One);
    public GamepadState Controller2 = new GamepadState(UserIndex.Two);

    //...

    public MyApplication() {

      Controller1.ButtonA_Pressed += Player1_ButtonA_Pressed;
      Controller1.ButtonA_Released += Player1_ButtonA_Released;
      //...
      Controller2.ThumbstickLeft_Changed += Player2_ThumbstickLeft_Changed;
      //...

    }

    static void Player1_ButtonA_Pressed(object sender, EventArgs e) {
      // Do stuff
    }
    // and so on...

    static void Player2_ThumbstickLeft_Changed(object sender, ThumbstickEventArgs e) {
      // Do stuff
    }
    //...

  }
}

State Update

C#
using System;
using SlimDX;
using SlimDX.XInput;
using SlimDXPlugin.XInput;

namespace MyApplication {

  public class MyApplication {

    //...

    // Simple Update
    public void Tick() {
      Controller1.Update();
      Controller2.Update();
    }

    // Better Update
    public void BetterTick() {
      if(Controller1.Connected) {
        Controller1.Update();
      }
      //...
    }
  }
}

That's basically all you need. Everything else explains itself. If not, take a look at the source or post your questions in the comments, I will respond as soon as possible.

Points of Interest

Sure, this is probably not the best solution, but at least it's something and for my own application just the right thing. But I hope Microsoft will implement a better solution within .NET.

History

  • Initial release (12/30/2014)
  • Swapped 7z for zip, code should be browseable now (12/31/2014)

License

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


Written By
Founder beStrange:Games
Germany Germany
Founder, Designer, Writer and Code Monkey at beStrange:Games

Currently working on Relink, a unofficial sequel to Uplink by Introversion Software

Comments and Discussions

 
NewsSource Download is coming soon(tm) Pin
Daniel Lieberwirth (BrainInBlack)31-Dec-14 1:53
professionalDaniel Lieberwirth (BrainInBlack)31-Dec-14 1:53 
GeneralMy vote of 3 Pin
Mansoor Alikhan K30-Dec-14 17:39
Mansoor Alikhan K30-Dec-14 17:39 
QuestionRe: My vote of 3 Pin
Daniel Lieberwirth (BrainInBlack)31-Dec-14 1:54
professionalDaniel Lieberwirth (BrainInBlack)31-Dec-14 1:54 
GeneralRe: My vote of 3 Pin
Mansoor Alikhan K1-Jan-15 5:33
Mansoor Alikhan K1-Jan-15 5:33 
I'm also buddy.. i mean that need much more information about your articles.

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.