SFML community forums

Help => General => Topic started by: ikarah on March 14, 2016, 09:00:01 pm

Title: Real Time Keyboard Input
Post by: ikarah 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.
Title: Re: Real Time Keyboard Input
Post by: Nexus on March 14, 2016, 09:01:05 pm
Use events, and disable key repetition. Everything is explained in the tutorials and documentation :)
Title: Re: Real Time Keyboard Input
Post by: ikarah 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.
Title: Re: Real Time Keyboard Input
Post by: bitano 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.
Title: Re: Real Time Keyboard Input
Post by: Nexus 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 (http://en.sfml-dev.org/forums/index.php?topic=5559.msg36368#msg36368) (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.