SFML community forums

Help => Window => Topic started by: SymmetryBreaking on May 26, 2015, 01:32:27 pm

Title: Multiple keyreleased events triggering per keyrelease
Post by: SymmetryBreaking on May 26, 2015, 01:32:27 pm
When running the following code in a loop if I simply press and release a key a single one time I find "Key released." printed well over 50 times:

void Game::GameLoop()
{
sf::Event currentEvent;
_mainWindow.pollEvent(currentEvent);

if (currentEvent.type == sf::Event::KeyReleased)
std::cout << "Key released." << std::endl;

}
 

while (!Exit())
{
GameLoop();
}
 

This is using SFML 2.3 with Microsoft Visual Studio Express 2013 with a release build.
Title: Re: Multiple keyreleased events triggering per keyrelease
Post by: eXpl0it3r on May 26, 2015, 01:36:09 pm
Because you don't actually check whether an event has been generated and yet still access the event, which leads to undefined behavior where the event type has a random value which then may match the value of sf::Event::KeyReleased.

Read the official tutorial on how to properly handle events. ;)
Title: Re: Multiple keyreleased events triggering per keyrelease
Post by: Nexus on May 26, 2015, 01:36:35 pm
You're doing event handling wrong. Please read the tutorials to see how it's done correctly ;)
Title: Re: Multiple keyreleased events triggering per keyrelease
Post by: SymmetryBreaking on May 26, 2015, 01:46:19 pm
Because you don't actually check whether an event has been generated and yet still access the event, which leads to undefined behavior where the event type has a random value which then may match the value of sf::Event::KeyReleased.

Read the official tutorial on how to properly handle events. ;)

D'oh. I knew it was something obvious. I'm a little disappointed it was that obvious, but nonetheless thank you. Works perfectly now.