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

Author Topic: Real Time Keyboard Input  (Read 2920 times)

0 Members and 1 Guest are viewing this topic.

ikarah

  • Newbie
  • *
  • Posts: 15
    • View Profile
Real Time Keyboard Input
« on: March 14, 2016, 09:00:01 pm »
I am using real time keyboard input to get immediate response within my game.

if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space) && x == 5) {
         std::cout << "space" << std::endl;
      }

The thing is I want it to take that input only one time. I tried using event from some reason it skips the keyboard input many times if I am using events but, this works perfectly except that it takes way too much input since its real time. Is there a way to make it take one spacebar hit and stop for a while lets say 2 seconds.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Real Time Keyboard Input
« Reply #1 on: March 14, 2016, 09:01:05 pm »
Use events, and disable key repetition. Everything is explained in the tutorials and documentation :)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

ikarah

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Real Time Keyboard Input
« Reply #2 on: March 14, 2016, 09:05:17 pm »
I tried that it did not  work the way I want it to. It simply ignores some of the input and accepts some. The game im writing uses a lot of window clear and display and for that reason I think it skips input from the keyboard. It works perfectly fine with the real time keyboard input except i do not want it to take continuous input.

bitano

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
Re: Real Time Keyboard Input
« Reply #3 on: March 15, 2016, 08:24:18 am »
Use a boolean vector <keyVec> and resize it to 255.

On keyPress check whether keyVec[sf::keyboard:<keyPressed>] is true. If so, ignore the keypress and if not, set it to true.
On keyRelease set keyVec[sf::keyboard:<keyReleased>] to false.

Nexus' method is better though as that's simply using already built-in functionality/logic.
« Last Edit: March 15, 2016, 09:15:26 am by bitano »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Real Time Keyboard Input
« Reply #4 on: March 15, 2016, 10:21:01 am »
The game im writing uses a lot of window clear and display and for that reason I think it skips input from the keyboard.
That's not a reason. Every SFML game uses a lot of clear + display, and it never leads to problems.

Please come up with a minimal complete example (read that post carefully). Otherwise it's not possible to give you meaningful responses. As bitano says, the functionality used in his approach is already provided by sf::Keyboard.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything