I am trying to check if I press and hold the A button on my xbox controller. Right now i have it so i can see if my button is being pressed but not being hold. This is the code i'm using right now to see if its being pressed.
private void Loop()
{
while (true)
{
var state = _controller.GetState();
var LX = state.Gamepad.LeftThumbX;
var LY = state.Gamepad.LeftThumbY;
var magnitude = Math.Sqrt(LX * LX + LY * LY);
if (magnitude > _deadzone)
{
MoveCursor(LX, LY * -1);
Thread.Sleep(20);
}
if (state.Gamepad.Buttons == GamepadButtonFlags.A)
{
LeftClick();
Thread.Sleep(100);
}
else if (state.Gamepad.Buttons == GamepadButtonFlags.B)
{
RightClick();
Thread.Sleep(100);
}
}
}
To tell button triggers (first pressed) versus button held you must save the state and compare with each frame. If the previous frame did not have the button down but you have it down this frame then your user just triggered the button. If the previous frame was down and it is still down, then the user is holding the button down. If the previous frame was down but it is no longer down, then the user released the button.
But the trick is you must save off the previous frame's button state so that you can compare it to the current button state.