Search code examples
c#visual-studiosharpdxxinput

Using an Xbox One controller in Visual Studio?


I am trying to get input from an Xbox One controller, but I just cannot get it to work. I tried SharpDX, but I could not add "SharpDX.XInput" after installing SharpDX from NuGet. I also tried XInputDotNet, and added the reference to XInputDotNetPure, and it would crash when I ran my program, with the error message: "An attempt was made to load a program with an incorrect format". If you know how I could fix this, or another way to get input from an Xbox One controller, I would greatly appreciate it if you could share that with me. Thanks!


Solution

  • Used the Sharpdx.XInput interface. You don't need to install any other interface to get XBOX controllers working. There is an issue with install in 3.0 packages if you have win 8.1. A new beta V4.0 was released, you should be able to get this through Nuget if you enable beta flag.

    Declare a Controller Object

    eg.

    private Controller m_playerControl;
    

    in your creation, use

     m_playerControl = new Controller(UserIndex.One); 
    

    or 2 etc 3, 4

    You can test if the xbox controller is connected with:

    m_playerControl.IsConnected
    

    To grab the state.

      m_state = m_playerControl.GetState();
            m_lx = m_state.Gamepad.LeftThumbX;
            m_ly = m_state.Gamepad.LeftThumbY;
            m_lt = m_state.Gamepad.LeftTrigger;
            m_lb = m_state.Gamepad.Buttons;
    
            m_rx = m_state.Gamepad.RightThumbX;
            m_ry = m_state.Gamepad.RightThumbY;
            m_rt = m_state.Gamepad.RightTrigger;
    

    Advise you also use the same state object and overlay your gamepads etc.