在WPF应用程序中读取Xbox控制器

本文关键字:Xbox 控制器 读取 WPF 应用程序 | 更新日期: 2023-09-27 18:14:06

可以在WPF应用程序中读取Xbox One控制器吗?

我正在用USB线连接它。我想从按钮得到布尔值,并能够从棍棒和触发器读取模拟值。我将使用这些值来控制Pololu 3pi机器人。

是否有一个简单的方法来实现?

在WPF应用程序中读取Xbox控制器

试试这个。Code4Fun可能对你的控制器有帮助。

[#CODING4FUN] #XboxOne游戏控制器+ c# =有趣的时间!

来自网站:

WPF应用程序代码的主要视图如下:

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