When implementing pressing Enter to restart the game, should I use Keyboard::IsKeyPressed or use Event::KeyReleased in some way? I am afraid that Keyboard::IsKeyPressed will restart the game in excess of hundred times.Because a briefest of pressed will last a significant fraction of second.
1 Answers
sf::Event::KeyPressed & sf::Event::KeyReleased are events triggerd when that thing is done. Where as isKeyPressed is a function that tells you if a key is pressed right now in that iteration of the window loop.
In general Events are used to do single things at a time. Everything else is usedd in each iteration. Any event trigger triggers a single event even if that event is ongoing throughout many iterations of the game loop.
A good example of usage of these two is character movement onscreen. Moving a character with an event event::Keypressed(Left) will act like a keyPress during writing text. Try holding down any letter on your keyboard in like a notepad. You'll see your single letter pop up once and then after delay multiple letters follow slowly.
With isKeypressed you have a sudden trigger thats continous until unfulfilled. For character movement isKeyPressed is the one to use.
For example button usage onscreen would be better done in events as you can hold that button down and only want a single click on it. event::Keypressed would be better then.
Also i recommend adding the event::Keypressed with another statement saying which key was pressed: event.type == sf::event::keyPressed && sf::keyboard::is::keypressed(LeftP) The above is used inside the event loop
you shouldnt use events outside the event loop. it would be pointless
For simple single key stroke usage use event key pressed or released. If you want to be fancy with eg a enter hold down for z seconds then iskepressed. It all boils down to like i said earlier, does it have to be continuous or can it be a single togglelike event

- 111
- 2
-
1Can you clarify your answer to the question "To restart the game when the player presses Enter, should I use
Event::KeyReleased
orKeyboard::IsKeyPressed
?" – DMGregory Sep 16 '22 at 19:46 -
I did clarify. Events are used for single togglelike things such as buttons on GUI, actions taken with window such as resize or single keystrokes. Where as is Key Pressed is used for any continuous and gradual usually motion. It depends what you actually want it to do. If your enter key is meant to be a single click then event key pressed or released. If you want to have the user hold down enter for specific time then is key pressed. Sorry if it it didn't seem clear enough from my answer. Sfml is all about what is it meant to do rather what's usually done. There are so many paths to take – Monogeon Sep 23 '22 at 06:27