Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Problem with keyboard pressed  (Read 2914 times)

0 Members and 1 Guest are viewing this topic.

alf.javier

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
Problem with keyboard pressed
« 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.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11035
    • View Profile
    • development blog
    • Email
AW: Problem with keyboard pressed
« Reply #1 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: AW: Problem with keyboard pressed
« Reply #2 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.

AFS

  • Full Member
  • ***
  • Posts: 115
    • View Profile
Re: Problem with keyboard pressed
« Reply #3 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;
 

 

anything