我正在使用 Microsoft DirectX 访问我的游戏手柄。这是一个类似这样的 USB 游戏手柄:
我可以知道何时按下按钮以及轴的模拟值......
问题是是否有办法知道何时按下模拟按钮(红灯亮)。
那可能吗?如何?
我会为您的项目推荐SlimDX或SharpDX。它们支持 DirectX API,而且非常简单。
瘦身:
using SlimDX.DirectInput;
创建一个新的 DirectInput 对象:
DirectInput input = new DirectInput();
然后是一个用于处理的 GameController 类:
public class GameController
{
private Joystick joystick;
private JoystickState state = new JoystickState();
}
并像这样使用它:
public GameController(DirectInput directInput, Game game, int number)
{
// Search for Device
var devices = directInput.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly);
if (devices.Count == 0 || devices[number] == null)
{
// No Device
return;
}
// Create Gamepad
joystick = new Joystick(directInput, devices[number].InstanceGuid);
joystick.SetCooperativeLevel(game.Window.Handle, CooperativeLevel.Exclusive | CooperativeLevel.Foreground);
// Set Axis Range for the Analog Sticks between -1000 and 1000
foreach (DeviceObjectInstance deviceObject in joystick.GetObjects())
{
if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0)
joystick.GetObjectPropertiesById((int)deviceObject.ObjectType).SetRange(-1000, 1000);
}
joystick.Acquire();
}
最后得到每个方法的状态:
public JoystickState GetState()
{
if (joystick.Acquire().IsFailure || joystick.Poll().IsFailure)
{
state = new JoystickState();
return state;
}
state = joystick.GetCurrentState();
return state;
}