0

I am trying to use a gamepad to control an application. It's not a game, just a plain application using Windows Forms. So it doesn't have a game loop/update process or anything like that. I wouldn't like to use XNA because I think it's a huge overload just to capture a button press.

I am experimenting with both SlimDX and SharpDX. As I understand, they are just wrappers for DirectX, right?

Looking at the documentation, it seems like there is no event for a button press. So I have been looking for an alternative. I have tried adding a timer (from the System.Windows.Forms.Timer class), and reading the state of the gamepad like this:

private void timer_tick(object sender, EventArgs e)
{
    State s = controller.GetState();
    stateLabel.Text = s.Gamepad.Buttons == GamepadButtonFlags.A ? "A" : "";
}

With a small enough interval between timer ticks (I'm using 10ms), I can see whether the button is pressed or not. However, I don't want to handle the button press multiple times should the button be held down - I need to make sure it has been released before handling the button press again. Between two ticks of the timer, I don't know if a button was pressed twice or if it was just being held down.

I thought about using the packet number in the controller state, but it will change at the slightest touch on an analog stick or shoulder trigger.

Help?

Geeky Guy
  • 9,229
  • 4
  • 42
  • 62
  • What are you feeding this information to? Something like a textbox? – bubbinator Mar 23 '14 at 19:44
  • Could be. I'm actually trying to hack a controller so that a paraplegic person could operate it with her mouth, and the controller would behave like a mouse. [Something like this](http://quadcontrol.com/) but more aimed towards general computer use rather than gaming. I know I could build a special mouse - but I've got plenty of spare controllers and I think I can do more with them than with a mouse. – Geeky Guy Mar 23 '14 at 19:48
  • Very noble cause. I'll see if I can cook something up to get you started. Looks like some clever `bool` usage will can get you started. – bubbinator Mar 23 '14 at 19:52

1 Answers1

3

From what I can gather after reading you question over a few times is that you are wanting to log a button press only 1 time until it is released. You probably should use the packet number technique to keep track of any changes to the other input.

To get a single input from the button being held down as opposed to getting continuous input (1 as opposed to 11111111...etc) create an old state an a current state. Then compare the old state with the new state.

Something like this:

class Input
{
    State old;

    void GetInput()
    {
        State new = controller.GetState();
        if (this.old.GamePad.Buttons == GamepadButtonFlags.A && new.GamePad.Buttons == GamepadButtonFlags.A)
        {
            // Do stuff that will be called only once.
        }
        this.old = new;
    }
}   
bubbinator
  • 487
  • 2
  • 7
  • Comparing states like this did the trick. I like this strategy that doesn't need to check for a packet number, because even the slightest touch on the sticks or triggers will generate a new packet. Thank you very much :) – Geeky Guy Mar 24 '14 at 03:28
  • Glad I could help. For the record, I based this off of some code that I have used for `Monogame`. – bubbinator Mar 24 '14 at 18:35