How to use xbox one controller in C# application

∥☆過路亽.° 提交于 2020-01-23 03:00:27

问题


There already exists a large amount of information for using an xbox 360 controller in C#, but I didn't find as much info for an xbox one controller.

I need very basic operation out of it, specifically the joystick and trigger values.

The majority of information online is for C++ applications, but rather than attempting to write a custom library, I'd like to use something like SharpDX. How do I use this in my application?

Note: I'm posting this to share info that I found, as well as documenting my findings for myself. I'd love to hear other methods of getting controller input into a .NET application though.


回答1:


The easiest way I found to get started was to use SharpDX. From the SharpDX website:

SharpDX is an open-source managed .NET wrapper of the DirectX API.

SharpDX is available on nuget, so it's very simple to get started with it inside Visual Studios.

To get started, go to Tools -> NuGet Package Manager -> Package Manager Console inside Visual Studios (I'm using 2015 community edition).

Then simply type: Install-Package SharpDX into the Package Manager Console that appears at the bottom of Visual Studios.

Visual Studios will then download and add it to your solution. Now onto the code.

Since I wanted input from the xbox controller, we just need to add:
using SharpDX.XInput to the top of our program.

And the code to get all the values is fairly simple:

class XInputController
{
    Controller controller;
    Gamepad gamepad;
    public bool connected = false;
    public int deadband = 2500;
    public Point leftThumb, rightThumb = new Point(0,0);
    public float leftTrigger, rightTrigger;

    public XInputController()
    {
        controller = new Controller(UserIndex.One);
        connected = controller.IsConnected;
    }

    // Call this method to update all class values
    public void Update()
    {
        if(!connected) 
            return;

        gamepad = controller.GetState().Gamepad;

        leftThumb.x  = (Math.Abs((float)gamepad.LeftThumbX ) < deadband) ?  0 : (float)gamepad.LeftThumbX  / short.MinValue * -100;
        leftThumb.y  = (Math.Abs((float)gamepad.LeftThumbY ) < deadband) ?  0 : (float)gamepad.LeftThumbY  / short.MaxValue * 100;
        rightThumb.y = (Math.Abs((float)gamepad.RightThumbX) < deadband) ?  0 : (float)gamepad.RightThumbX / short.MaxValue * 100;
        rightThumb.x = (Math.Abs((float)gamepad.RightThumbY) < deadband) ?  0 : (float)gamepad.RightThumbY / short.MaxValue * 100;

        leftTrigger  = gamepad.LeftTrigger;
        rightTrigger =  gamepad.RightTrigger;
    }
}

This creates a deadband because the joysticks on the controller never fully return to zero. It also inverts the left X axis so that both stick inputs match. Y positive is up, X positive is to the right:

leftThumb.x  = (Math.Abs((float)gamepad.LeftThumbX ) < deadband) ?  0 : (float)gamepad.LeftThumbX  / short.MinValue * -100;
leftThumb.y  = (Math.Abs((float)gamepad.LeftThumbY ) < deadband) ?  0 : (float)gamepad.LeftThumbY  / short.MaxValue * 100;
rightThumb.y = (Math.Abs((float)gamepad.RightThumbX) < deadband) ?  0 : (float)gamepad.RightThumbX / short.MaxValue * 100;
rightThumb.x = (Math.Abs((float)gamepad.RightThumbY) < deadband) ?  0 : (float)gamepad.RightThumbY / short.MaxValue * 100;

Hopefully this will be helpful to someone in the future looking to add an xbox one controller input to their .NET application!



来源:https://stackoverflow.com/questions/39109609/how-to-use-xbox-one-controller-in-c-sharp-application

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!