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

Author Topic: Multiple keyreleased events triggering per keyrelease  (Read 2680 times)

0 Members and 1 Guest are viewing this topic.

SymmetryBreaking

  • Newbie
  • *
  • Posts: 2
    • View Profile
Multiple keyreleased events triggering per keyrelease
« 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.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Multiple keyreleased events triggering per keyrelease
« Reply #1 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. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Multiple keyreleased events triggering per keyrelease
« Reply #2 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 ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

SymmetryBreaking

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Multiple keyreleased events triggering per keyrelease
« Reply #3 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.