Toggle an input

前端 未结 1 1070
名媛妹妹
名媛妹妹 2021-01-28 18:07

I am using SlimDX to use my Xbox 360 controller and I would like a way of when pressing a button on the controller the state changes to on and stays on but when I press it again

相关标签:
1条回答
  • 2021-01-28 18:48

    You may use a bool to determine if the button is toggled on or off. You will also need to know the gamepad's previous state, so the bool won't toggle all the time, just because you keep pressing the button.

    bool myCommand = false; // declare the bool
    GamePadState oldState; // you need to know the previous state of your gamepad
    
    public void Update()
    {
        if (GamePad.GetState().KeyPressed == /*key*/ && oldState.KeyPressed != /*key*/)
            myCommand = !myCommand;
        oldState = GamePad.GetState();
    }
    

    Keep in mind, that you may need to use something other than GamePad or GamePadState, as this is merely pseudo-code

    0 讨论(0)
提交回复
热议问题