使用 XInput 和 VB.NET 来支持 xbox 360 控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21344566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 16:34:06  来源:igfitidea点击:

Use XInput and VB.NET for xbox 360 controller support

c#vb.netxinput

提问by Cody

I am wondering if anyone might be able to give me a little help with xinput (for and Xbox controller) and vb.net.

我想知道是否有人可以在 xinput(用于和 Xbox 控制器)和 vb.net 方面给我一些帮助。

What I am trying to do is translate the analog stick motions to certain events in my application. Specifically what I am trying to do is move a map when the joystick is moved. Is it possible to use the xinput with vb.net? I'm stuck in either vb or c# based on what the COM libraries are for the application. Any attempts to add the DLL's just throws an error at me too.

我想要做的是将模拟摇杆运动转换为我的应用程序中的某些事件。具体来说,我想要做的是在移动操纵杆时移动地图。是否可以将 xinput 与 vb.net 一起使用?根据应用程序的 COM 库,我被困在 vb 或 c# 中。任何添加 DLL 的尝试也会向我抛出错误。

Thanks for any help,

谢谢你的帮助,

Cheers!

干杯!

回答by pieter_dv

You can also look into http://sharpdx.org/

您还可以查看http://sharpdx.org/

It's a managed XInput library. Connecting to a controller is easy:

它是一个托管的 XInput 库。连接到控制器很容易:

var controller = new SharpDX.XInput.Controller(SharpDX.XInput.UserIndex.One);
if (controller.IsConnected)
{
    var state = controller.GetState();
    var x = state.Gamepad.LeftThumbX;
    var y = state.Gamepad.LeftThumbY;
}

SharpDX is available as nuget package

SharpDX 可用作 nuget 包

回答by Aybe

You can achieve that with MonoGame, here's a small example of a WPF application reading the value of the left thumb stick.

您可以使用 MonoGame 实现这一点,这里有一个 WPF 应用程序读取左拇指摇杆值的小示例。

enter image description here

在此处输入图片说明

<Window x:Class="GamePadTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TextBlock x:Name="TextBlock1"></TextBlock>
    </Grid>
</Window>

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace GamePadTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            CompositionTarget.Rendering += CompositionTarget_Rendering;
        }

        private void CompositionTarget_Rendering(object sender, EventArgs e)
        {
            var gamePadState = GamePad.GetState(PlayerIndex.One);
            TextBlock1.Text = gamePadState.ThumbSticks.Left.ToString();
        }
    }
}

How did I do that ?

我是怎么做到的?

  • Installed MonoGame : https://monogame.codeplex.com/
  • Created a new WPF project and referenced "C:\Program Files (x86)\MonoGame\v3.0\Assemblies\Windows8\MonoGame.Framework.dll"
  • 已安装 MonoGame:https://monogame.codeplex.com/
  • 创建了一个新的 WPF 项目并引用了“C:\Program Files (x86)\MonoGame\v3.0\Assemblies\Windows8\MonoGame.Framework.dll”

Do not forget to copy SDL.DLL to your bin\Debug, grab it here: http://www.libsdl.org/release/SDL-1.2.15-win32.zip

不要忘记将 SDL.DLL 复制到您的 bin\Debug,从这里获取:http: //www.libsdl.org/release/SDL-1.2.15-win32.zip

(done under Windows 8 x64 + Visual Studio 2012)

(在 Windows 8 x64 + Visual Studio 2012 下完成)