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

XBOX 360 Controller Class

30 Dec 2014 1  
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

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

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

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 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