简体   繁体   English

SharpDX.Xinput检查是否按住A按钮而不是按下按钮

[英]SharpDX.Xinput Check if A button is being held down instead of being pressed

I am trying to check if I press and hold the A button on my xbox controller. 我正在尝试检查是否按住我的xbox控制器上的A按钮。 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. 但是诀窍是您必须保存上一帧的按钮状态,以便可以将其与当前按钮状态进行比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM