1
\$\begingroup\$

I have a while loop that goes for ever. The goal is if I press A button, a message should be printed out showing this button has been pressed A has been pressed. I've done the loop and connecting XBox controller but the problem is that the message is printed out many times if I press the button one time. My question is why the message is being shown many times while I just press the button one time? Should I make some kind of sleep?


Edit: This is the part of the code

while(true)
    {
        if(Player->IsConnected())
        {
            if( Player->GetState().Gamepad.wButtons & XINPUT_GAMEPAD_A )
                std::cout << " A button has been pressed ..." << std::endl;
        }
        else
        {
            std::cout << "\nERROR: Connect XBox Controller ... \n";
            std::cout << "Press Any Key To Exit.";
            std::cin.get();
            break;
        }
    }

The result when I only press once.

Press A to be printed out ...
 A button has been pressed ...
 A button has been pressed ...
 A button has been pressed ...
 A button has been pressed ...
 A button has been pressed ...
 A button has been pressed ...
 A button has been pressed ...
 A button has been pressed ...
 A button has been pressed ...
 A button has been pressed ...
 A button has been pressed ...
 A button has been pressed ...
 A button has been pressed ...
 A button has been pressed ...
 A button has been pressed ...
 A button has been pressed ...
 A button has been pressed ...
 A button has been pressed ...
 A button has been pressed ...
 A button has been pressed ...
 A button has been pressed ...
 A button has been pressed ...
 A button has been pressed ...
 A button has been pressed ...
 A button has been pressed ...
 A button has been pressed ...
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Can you provide some of your code? \$\endgroup\$ Aug 16, 2014 at 6:56
  • \$\begingroup\$ @user000user, I've updated the post. \$\endgroup\$
    – CroCo
    Aug 16, 2014 at 7:17
  • \$\begingroup\$ For more clarification: Does the message stop printing once you release the button? If so, user00user's solution is enough. If not, then there's some different issue with polling (as I suggested). \$\endgroup\$
    – Mario
    Aug 16, 2014 at 8:13

1 Answer 1

3
\$\begingroup\$

The reason, why the message is printed several times, is, due to the fact, that you read the state of the button every loop. Player->GetState().Gamepad.wButtons gives you the current state of the buttons. That means, as long as a button is pressed, requesting the state and checking for that button will be true.

What you can do, is, declare a boolean variable on top of the loop, that saves the state of a specified button of the last update.

This could look similar to this.

bool btnADown = false;

...

while(true)
{
    if(Player->IsConnected())
    {
        if( Player->GetState().Gamepad.wButtons & XINPUT_GAMEPAD_A )
        {
            if(!btnADown)
            {
                std::cout << " A button has been pressed ..." << std::endl;
                btnADown = true;
            }
        }
        else
        {
            btnADown = false;
        }
    }
    else
    {
        std::cout << "\nERROR: Connect XBox Controller ... \n";
        std::cout << "Press Any Key To Exit.";
        std::cin.get();
        break;
    }
}
\$\endgroup\$
7
  • \$\begingroup\$ thanks it worked but I still don't understand why. Is there another way to overcome this problem? Or should sleep function do the trick? \$\endgroup\$
    – CroCo
    Aug 16, 2014 at 7:54
  • \$\begingroup\$ This might just be working around the actual issue. Does this work on multiple key presses, i.e. is btnADown = false; ever executed? \$\endgroup\$
    – Mario
    Aug 16, 2014 at 8:12
  • \$\begingroup\$ @CroCo This works, because once the button is down, btnADown becomes true and thus, the message will not be printed anymore, until the key has been released and the else part will be executed/ btnADown will become false. A sleep function should be avoided, because you never know, how long a user will hold down a button. \$\endgroup\$ Aug 16, 2014 at 8:15
  • \$\begingroup\$ @Mario This does work for multiple key presses. If the button XINPUT_GAMEPAD_A is not pressed, the if statement will become false and thus the else statement will be executed. \$\endgroup\$ Aug 16, 2014 at 8:18
  • 1
    \$\begingroup\$ Right, because depending on the speed of your program, the loop will be executed several times during a single key press. \$\endgroup\$ Aug 16, 2014 at 8:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .