使用C#.NET从操纵杆输入

我在Google上搜索了这个,但我想出的唯一的东西已经过时而且没有用。

有没有人有关于如何使用C#.NET获取操纵杆数据的任何信息?

一:使用SlimDX 。

二:它看起来像这样( GamepadDevice是我自己的包装器,代码缩小到相关部分)。

找到操纵杆/垫GUID:

  public virtual IList Available() { IList result = new List(); DirectInput dinput = new DirectInput(); foreach (DeviceInstance di in dinput.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly)) { GamepadDevice dev = new GamepadDevice(); dev.Guid = di.InstanceGuid; dev.Name = di.InstanceName; result.Add(dev); } return result; } 

用户从列表中选择后,获取游戏手柄:

  private void acquire(System.Windows.Forms.Form parent) { DirectInput dinput = new DirectInput(); pad = new Joystick(dinput, this.Device.Guid); foreach (DeviceObjectInstance doi in pad.GetObjects(ObjectDeviceType.Axis)) { pad.GetObjectPropertiesById((int)doi.ObjectType).SetRange(-5000, 5000); } pad.Properties.AxisMode = DeviceAxisMode.Absolute; pad.SetCooperativeLevel(parent, (CooperativeLevel.Nonexclusive | CooperativeLevel.Background)); pad.Acquire(); } 

轮询垫看起来像这样:

  JoystickState state = new JoystickState(); if (pad.Poll().IsFailure) { result.Disconnect = true; return result; } if (pad.GetCurrentState(ref state).IsFailure) { result.Disconnect = true; return result; } result.X = state.X / 5000.0f; result.Y = state.Y / 5000.0f; int ispressed = 0; bool[] buttons = state.GetButtons(); 

由于这是我在研究C#中的操纵杆/游戏手柄输入时获得谷歌的最大热门,我想我应该发布一个响应给别人看。

我发现最简单的方法是使用SharpDX和DirectInput。 你可以通过NuGet(SharpDX.DirectInput)安装它

在那之后,只需要调用一些方法:

SharpDX的示例代码

 static void Main() { // Initialize DirectInput var directInput = new DirectInput(); // Find a Joystick Guid var joystickGuid = Guid.Empty; foreach (var deviceInstance in directInput.GetDevices(DeviceType.Gamepad, DeviceEnumerationFlags.AllDevices)) joystickGuid = deviceInstance.InstanceGuid; // If Gamepad not found, look for a Joystick if (joystickGuid == Guid.Empty) foreach (var deviceInstance in directInput.GetDevices(DeviceType.Joystick, DeviceEnumerationFlags.AllDevices)) joystickGuid = deviceInstance.InstanceGuid; // If Joystick not found, throws an error if (joystickGuid == Guid.Empty) { Console.WriteLine("No joystick/Gamepad found."); Console.ReadKey(); Environment.Exit(1); } // Instantiate the joystick var joystick = new Joystick(directInput, joystickGuid); Console.WriteLine("Found Joystick/Gamepad with GUID: {0}", joystickGuid); // Query all suported ForceFeedback effects var allEffects = joystick.GetEffects(); foreach (var effectInfo in allEffects) Console.WriteLine("Effect available {0}", effectInfo.Name); // Set BufferSize in order to use buffered data. joystick.Properties.BufferSize = 128; // Acquire the joystick joystick.Acquire(); // Poll events from joystick while (true) { joystick.Poll(); var datas = joystick.GetBufferedData(); foreach (var state in datas) Console.WriteLine(state); } } 

我希望这有帮助。

我甚至可以使用DualShock3和MotioninJoy驱动程序。

坏消息是,微软似乎不再支持他们的DirectX NET库,而是专注于XNA。 我不在GameDev工作,所以我不需要使用XNA,但如果你开发电脑游戏,你可以试试。 好消息是还有其他方法。 一个是SlimDX新框架,可以帮助您从C#中使用DirectX。 另一种方法是直接将“Microsoft.DirectX.dll”和“Microsoft.DirectX.DirectInput.dll”的引用添加到您的项目中。 你可以找到它们“.. \ Windows \ Microsoft.NET \ DirectX for Managed Code \”。 如果你打算使用最后一种方法,这里有一个指向codeproject的链接,你可以在其中阅读如何使用操纵杆。

编辑:如果您的应用程序基于新版本的.NET版本,那么该应用程序可能会挂起。 要解决此问题,请更改配置文件并添加以下内容:

  

谷歌引领我到这里,虽然不是这个问题的要求, OpenTK是Windows和Linux(单声道)支持的一个很好的选择。

从OpenTK文档 ,此代码适用于Raspberry Pi + Raspbian + Mono 3.12.0:

 for (int i = 0; i < 4; i++) { var state = Joystick.GetState(i); if (state.IsConnected) { float x = state.GetAxis(JoystickAxis.Axis0); float y = state.GetAxis(JoystickAxis.Axis1); // Print the current state of the joystick Console.WriteLine(state); } }