2013-04-24 91 views
3

我正在使用Microsoft DirectX访问我的手柄。这是一个USB游戏手柄像这样的:操纵杆+ c#:模拟按钮

enter image description here

我能得到访问时,按下按钮,并在轴的模拟值就知道...

的事情是,如果有一个方法知道何时按下模拟按钮(红灯亮)。

这可能吗?怎么样?

+0

你的意思是:*模拟按键*,你说的是L2/R2,或者你指的是压敏面对按钮触发? – Nolonar 2013-04-24 13:11:41

+0

我的意思是启用x,y,z轴电位器的按钮。打开红灯的那个...... – 2013-04-24 13:49:49

+2

不,这可能是不可能的,因为它不是一个输入按钮,它是一个改变硬件工作方式的按钮。 – 2013-04-24 15:08:32

回答

2

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

SlimDX

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

是的,我在其结构中使用类似的东西。只需使用Microsoft Direct X ...为什么SLIM/SHARP更好?可以了解游戏手柄的红灯状态? – 2013-04-24 13:51:06

+0

这是一个很好的包装,我更喜欢框架的一些功能。对于红灯我建议它将包含在joystick.GetObjects()中。 – Smartis 2013-04-24 14:09:11

+0

谢谢@Smartis我会检查joystick.GetObjects()...我需要知道模拟是否为了知道z轴是否可用... – 2013-04-24 14:25:06