3

Is it possible to read an Xbox One controller in a WPF application?

I'm connecting it through a USB cable. I'd like to get boolean values from buttons and be able to read analog values from sticks and triggers. I will be using those values to control a Pololu 3pi robot.

Is there a simple way to achieve that?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nollie
  • 141
  • 2
  • 11
  • 1
    You could use [SharpDX's](http://sharpdx.org/) DirectInput or similiar. This one allows for relatively easy interaction of any input device. – SharpShade Oct 21 '16 at 11:20
  • In case the Xbox controller does not use DirectInput and it uses default USB drivers you can use https://msdn.microsoft.com/en-us/library/windows/hardware/ff540174%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396 – Cleptus Oct 21 '16 at 11:33

1 Answers1

6

Try this. Code4Fun might help you with your controller.

[#CODING4FUN] #XboxOne Game Controller + C# = fun time!

From the website:

The main view of the WPF application code is the following.

using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Threading;
using SharpDX.XInput;

namespace ElBruno.GameController
{
    public partial class MainWindow : INotifyPropertyChanged
    {
        DispatcherTimer _timer = new DispatcherTimer();
        private string _leftAxis;
        private string _rightAxis;
        private string _buttons;
        private Controller _controller;

    public MainWindow()
    {
        DataContext = this;
        Loaded += MainWindow_Loaded;
        Closing += MainWindow_Closing;
        InitializeComponent();
        _timer = new DispatcherTimer {Interval = TimeSpan.FromMilliseconds(100)};
        _timer.Tick += _timer_Tick;
        _timer.Start();
    }

    void _timer_Tick(object sender, EventArgs e)
    {
        DisplayControllerInformation();
    }

    void DisplayControllerInformation()
    {
        var state = _controller.GetState();
        LeftAxis = string.Format("X: {0} Y: {1}", state.Gamepad.LeftThumbX, state.Gamepad.LeftThumbY);
        RightAxis = string.Format("X: {0} Y: {1}", state.Gamepad.RightThumbX, state.Gamepad.RightThumbX);
        //Buttons = string.Format("A: {0} B: {1} X: {2} Y: {3}", state.Gamepad.Buttons.ToString(), state.Gamepad.LeftThumbY);
        Buttons = string.Format("{0}", state.Gamepad.Buttons);

    }

    void MainWindow_Closing(object sender, CancelEventArgs e)
    {
        _controller = null;
    }

    void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        _controller = new Controller(UserIndex.One);
        if (_controller.IsConnected) return;
        MessageBox.Show("Game Controller is not connected ... you know ;)");
        App.Current.Shutdown();
    }

    #region Properties

    public string LeftAxis
    {
        get
        {
            return _leftAxis;
        }
        set
        {
            if (value == _leftAxis) return;
            _leftAxis = value;
            OnPropertyChanged();
        }
    }

    public string RightAxis
    {
        get
        {
            return _rightAxis;
        }
        set
        {
            if (value == _rightAxis) return;
            _rightAxis = value;
            OnPropertyChanged();
        }
    }

    public string Buttons
    {
        get
        {
            return _buttons;
        }
        set
        {
            if (value == _buttons) return;
            _buttons = value;
            OnPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        var handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }

    #endregion
}
}
pix
  • 1,264
  • 19
  • 32