3

I am using Microsoft DirectX to get access to my gamepad. This is an usb gamepad like this one:

enter image description here

I could get access to know when buttons are pressed and also the analog values of the axis...

The thing is if there is a way to know when the analog button is pressed (Red light on).

Is that possible? How?

Ankur
  • 5,086
  • 19
  • 37
  • 62
the_moon
  • 553
  • 1
  • 7
  • 21
  • What do you mean: *analog button*, are you talking about the triggers L2 / R2, or are you referring to the pressure sensitive face buttons? – Nolonar Apr 24 '13 at 13:11
  • I mean the button that enables the potentiometers for the x,y,z axis. The one that turns on the red light... – the_moon Apr 24 '13 at 13:49
  • 2
    No, this is probably not possible since its not an Input button, its a button that alters the way the hardware works. – Robert J. Apr 24 '13 at 15:08

1 Answers1

2

I would recommend SlimDX or SharpDX for your Project. They support the DirectX API and are really simple.

SlimDX:

using SlimDX.DirectInput;

Create a new DirectInput-Object:

DirectInput input = new DirectInput();

Then a GameController Class for Handling:

public class GameController
{
    private Joystick joystick;
    private JoystickState state = new JoystickState();
}

And use it like this:

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

Finally get per Method the State:

public JoystickState GetState()
{
    if (joystick.Acquire().IsFailure || joystick.Poll().IsFailure)
    {
        state = new JoystickState();
        return state;
    }

    state = joystick.GetCurrentState();

    return state;
}
  • Yes, i am using something similar in its structure. Just using Microsoft Direct X... Why SLIM/SHARP are better? Could get knowledge of the state of the red light of the gamepad? – the_moon Apr 24 '13 at 13:51
  • It's a good wrapper and i prefer some features of the Framework. For the Red Light i suggest it will contain in joystick.GetObjects(). – Smartis has left SO again Apr 24 '13 at 14:09
  • Thanks @Smartis I will check joystick.GetObjects() for that... I need to know if the analog is on in order to know if z axis is available... – the_moon Apr 24 '13 at 14:25
  • What are Game and number in GameController method for? Game object isn't recognized by VS... – the_moon Apr 24 '13 at 21:17
  • In this Case it's the Game Object is from XNA and sorry GameController Class will i edit now... – Smartis has left SO again Apr 25 '13 at 09:19