操纵杆+ c#:模拟按钮

时间:2013-04-24 13:02:37

标签: c# directx joystick gamepad

我正在使用Microsoft DirectX来访问我的游戏手柄。这是一个像这样的USB游戏手柄:

enter image description here

我可以访问按钮按下的时间以及轴的模拟值......

问题是,如果有办法知道何时按下模拟按钮(红灯亮)。

这可能吗?怎么样?

1 个答案:

答案 0 :(得分:2)

我会为您的项目推荐SlimDXSharpDX。它们支持DirectX API,非常简单。

<强> SlimDX

using SlimDX.DirectInput;

创建一个新的DirectInput-Object:

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;
}