使用 C# .NET 从操纵杆获取输入 [英] Taking input from a joystick with C# .NET
本文介绍了使用 C# .NET 从操纵杆获取输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在谷歌上搜索了这个,但我想出的唯一的东西已经过时并且不起作用.
I searched around on Google for this, but the only things I came up with were outdated and did not work.
有人知道如何使用 C# .NET 获取操纵杆数据吗?
Does anyone have any information on how to get joystick data using C# .NET?
推荐答案
一:使用SlimDX.
二:它看起来像这样(其中 GamepadDevice
是我自己的包装器,并且代码被精简到只有相关部分).
Two: it looks something like this (where GamepadDevice
is my own wrapper, and the code is slimmed down to just the relevant parts).
找到操纵杆/键盘 GUID:
Find the joystick / pad GUIDs:
public virtual IList<GamepadDevice> Available()
{
IList<GamepadDevice> result = new List<GamepadDevice>();
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;
}
一旦用户从列表中选择,获取游戏手柄:
Once the user has selected from the list, acquire the gamepad:
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# .NET 从操纵杆获取输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文