SFML community forums

Help => Window => Topic started by: alf.javier on March 24, 2014, 06:35:25 am

Title: Problem with keyboard pressed
Post by: alf.javier on March 24, 2014, 06:35:25 am
In the game, the user can add money to his wallet. But when I press the key "C", the key are press many times, I want press only one time.

Quote
if (sf::Keyboard::isKeyPressed(sf::Keyboard::C))
      {
         creditos = creditos + 100;
      }

Can somebody help me with this problem?

Thanks.
Title: AW: Problem with keyboard pressed
Post by: eXpl0it3r on March 24, 2014, 07:46:49 am
You might want to use events instead. With events you'll also get some repeating, but only after a delay and if you use the release event it will get detected only once.
Title: Re: AW: Problem with keyboard pressed
Post by: Jesper Juhl on March 24, 2014, 08:18:36 am
You might want to use events instead. With events you'll also get some repeating, but only after a delay and if you use the release event it will get detected only once.
And you can disable key-repeat for the events as well.
Title: Re: Problem with keyboard pressed
Post by: AFS on March 25, 2014, 02:46:45 pm
Alternatively, you can make a simple class that holds the state of the key pressed from both the actual frame and the previous one, and depending of those values you can tell if it has been released, pressed, etc.

myKey.update( sf::Keyboard::isKeyPressed(sf::Keyboard::C) );

if ( myKey.wasReleased() )
     creditos = creditos + 100;