C# DirectInput - 确定游戏 handle Controller 类型

标签 c# directinput

我正在尝试使用 DirectInput API 在我的应用程序中使用各种游戏 handle Controller 。据我所知,轴在设备之间的映射不同。

除了使用 DeviceInformation.ProductName/InstanceName 来区分 Controller 以允许不同的轴控制分配之外,是否有确定 Controller 类型的通用实践方法?

最佳答案

如果您只需要知道 Controller 类型(FirstPerson、Joystick、Gamepad...),请使用 DeviceInformation.DeviceType。

要知道连接了哪个特定 Controller ,请使用 ProductName 或更好的 ProductGuid。对于两个不同的 Controller ,ProductName 可能相同。另一方面,即使对于相同生产费用的两个 Controller ,ProductGuid 也可能不同。

只有使用 ProductGuid,您才能毫无疑问地识别连接的设备实例。

人们可能会想通过 dev.GetObjects(DeviceObjectTypeFlags.Axis) 进行 foreach 并检查轴的名称(和偏移量),但不能保证名为“X 轴”的轴确实映射到 someDevice.CurrentJoystickState.X

您必须事先为要连接的任何 Controller 完成所有映射(只有您自己开发应用程序时才可以接受)并将该信息存储在应用程序的配置文件中,否则您将需要提供某种形式,用户可以自己映射新设备。我目前正在开发这个,但这个答案会超出这个问题的范围......

另请考虑,为了获取此基本信息(如 ProductGuid),您不必创 build 备。这些属性已经在 DeviceInstance 中可用(在 Microsoft.DirectX.DirectInput.Manager.GetDevices() 的 DeviceList 中)...


更新:因为我自己的回答一直困扰着我,所以我更深入地探讨了这个话题。
所以这是一种确定哪些轴映射到哪些值的方法:

// loop through the axes of an acquired device:
foreach (DeviceObjectInstance doi in 
    _currentDevice.GetObjects(DeviceObjectTypeFlags.Axis))
{
    doi.Name; // the name of the axis, e.g. 'X-Achse' (that's a german device...)
    doi.Offset / 4; // the enumeration 'index' of the axis
    doi.UsagePage; // the UsagePage determines the context of the axis value
    // vvvv
    doi.Usage; // the Usage tells you which JoystickState field to map to.
    // ^^^^
}

到目前为止,我发现了这些使用值(value)表示:

(with JoystickState s = _currentDevice.CurrentJoystickState)
  Usage   Axis value
   48      s.X
   49      s.Y
   50      s.Z
   51      s.Rx
   52      s.Ry
   53      s.Rz
   54      s.GetSlider()[0]

所以使用switch(doi.Usage)你可以自动获取相应的轴值。

关于C# DirectInput - 确定游戏 handle Controller 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3246163/

相关文章:

c# - Mvc 4 捆绑和 base64 svg 图像

c# - 如何在 C# 中编写条件锁?

c# - 如何将其他命名空间导入 Visual C#?

windows - DirectInput 模拟摇杆范围

c++ - DirectInput 未解析的外部符号

c# - JIT 编译器如何编译泛型?

c# - 如何在c#中获取按下的键盘按键

c# - 如何使用 nhibernate 选择字典值中的列?

c# - 如何使用 directInput 发送释放 key ?