SFML community forums

Help => Window => Topic started by: Jebbs on August 04, 2012, 08:59:59 pm

Title: Events tiggering when they shouldn't!(SFML 1.6)
Post by: Jebbs on August 04, 2012, 08:59:59 pm
Hey guys!

I'm having a pretty interesting issue. I have a particular even that I use to close the window simply by a key press, but the thing is that it sometimes get's triggered when I don't even press any keys!
This isn't the only event that triggers itself either! I have a different event that saves a screenshot of the program running that also has gone off by itself.
Any thoughts?

The code in question.
if(Event.Key.Code == sf::Key::Escape)
{
       App.Close();
}


if (Event.Key.Code == sf::Key::F1)
{
      sf::Image Screen = App.Capture();
      Screen.SaveToFile("screenshot.jpg");
}
Title: Re: Events tiggering when they shouldn't!(SFML 1.6)
Post by: eXpl0it3r on August 04, 2012, 09:05:26 pm
Have you taken a look at the tutorial on events (http://www.sfml-dev.org/tutorials/1.6/window-events.php)? ;)

Although you didn't post all the code I'm going to assume that you don't have the following code infront of the two if statements:
if(Event.Type == sf::Event::KeyPressed)
{
   // Your ifs
}
Not checking the event type but accessing Key.Code will lead to undefined behaviour.

Additionally I advice you to use SFML 2.
Title: Re: Events tiggering when they shouldn't!(SFML 1.6)
Post by: Jebbs on August 04, 2012, 09:13:41 pm
Nope! And I believe that should fix it.

Although the code I was using I took from the tutorials. The code for the screenshot for example came from this one :http://www.sfml-dev.org/tutorials/1.6/graphics-window.php (http://www.sfml-dev.org/tutorials/1.6/graphics-window.php)

The code for closing the program was just that but a little different.


Thanks for the reply though. I'll be sure to change it to make certain I check for an actual key press first!
Title: Re: Events tiggering when they shouldn't!(SFML 1.6)
Post by: eXpl0it3r on August 04, 2012, 09:20:55 pm
Although the code I was using I took from the tutorials. The code for the screenshot for example came from this one...
I see, yeah Laurent maybe should've added the type check there too. He probably thought the readers will already be familiar with events from the other tutorial and since it isn't the main topic it would be okay to just not explain it further/add all the neded code. ;)