SFML community forums

Help => Window => Topic started by: MichX on February 08, 2014, 02:30:44 pm

Title: Different between sf::Event::KeyPressed and sf::Keyboard::isKeyPressed(...)
Post by: MichX on February 08, 2014, 02:30:44 pm
Hi,
Could anyone explain me what is a different between check event by KeyPressed and check the same event by isKeyPressed outside the loop of events?
I mean when should i use first method and when secound?
When i tried check differences the results were the same so I don't understand it ;/
best regards!
Title: Re: Different between sf::Event::KeyPressed and sf::Keyboard::isKeyPressed(...)
Post by: G. on February 08, 2014, 02:43:31 pm
The KeyPressed event is triggered at the moment a key is pressed. It doesn't care how long the key is pressed, it only cares about the moment a key is pressed.

isKeyPressed doesn't care about the moment a key is pressed, it only cares about whether the key is currently pressed or not.

Use the event when you want to trigger something when a key is pressed.
Use real time inputs (sf::Keyboard, Mouse, etc.) when you want to get the state of something.

Note that the KeyPressed event may be repeated when you hold a key, at a frequency defined by your OS. You can however disable this behavior with the sf::Window::setKeyRepeatEnabled (http://www.sfml-dev.org/documentation/2.1/classsf_1_1Window.php#aef9f2b14c10ecba8a8df95dd51c5bb73) function.
Title: Re: Different between sf::Event::KeyPressed and sf::Keyboard::isKeyPressed(...)
Post by: MichX on February 08, 2014, 03:29:20 pm
Thanks a lot @G.
I will try to understand what u said ;-)
Title: Re: Different between sf::Event::KeyPressed and sf::Keyboard::isKeyPressed(...)
Post by: Jesper Juhl on February 08, 2014, 04:30:50 pm
It's really very simple.
When you press a key an event is put into a queue and waits there until you empty the queue by calling waitEvent() or pollEvent() , at which time you then notice that a key was pressed some time in the past (usually a few ms ago if you process the event queue every frame).
On the other hand, isKeyPressed() checks if the key is in fact pressed this very instant. So if the user pressed and released the key a few ms previously but is not holding the key down right now then you won't notice.
Title: Re: Different between sf::Event::KeyPressed and sf::Keyboard::isKeyPressed(...)
Post by: Hapax on February 08, 2014, 05:32:40 pm
One other thing to note is that by using the KeyPressed event, there is no way to know if that key is currently being pressed. For example, if you need to know that the key specifically isn't being pressed, you would need to use isKeyPressed for that.
Title: Re: Different between sf::Event::KeyPressed and sf::Keyboard::isKeyPressed(...)
Post by: Jesper Juhl on February 10, 2014, 11:43:42 pm
Well, you also get key released events, so it is perfectly possible to keep track of whether a key is still pressed (modulo some event delivery/processing time, but that's negligible).
Title: Re: Different between sf::Event::KeyPressed and sf::Keyboard::isKeyPressed(...)
Post by: Hapax on February 11, 2014, 01:29:48 am
Well, you also get key released events, so it is perfectly possible to keep track of whether a key is still pressed (modulo some event delivery/processing time, but that's negligible).
Oh, that's true. Rarely very useful though  :P
Title: Re: Different between sf::Event::KeyPressed and sf::Keyboard::isKeyPressed(...)
Post by: zsbzsb on February 11, 2014, 01:52:50 am
Oh, that's true. Rarely very useful though  :P

It is the best way in most circumstances to handle this. In fact this is the way the SFML Game Development book handles it.
Title: Re: Different between sf::Event::KeyPressed and sf::Keyboard::isKeyPressed(...)
Post by: Nexus on February 11, 2014, 11:38:40 am
As SFML provides real-time input out of the box, I would say it (and not events) is the default approach to query the real-time input state. Otherwise you constantly have to write abstractions on top of it, and the differences are only notable in few scenarios.

The SFML Game Development book only does it that way in the beginning, so that the start is kept simple and we don't have to explain the difference between real-time and event-based input in the first chapter ;)
Title: Re: Different between sf::Event::KeyPressed and sf::Keyboard::isKeyPressed(...)
Post by: wintertime on February 11, 2014, 04:27:25 pm
The problem with asking the current status is loosing information about keypresses and short keyreleases inbetween testing. Thats why the events are imo the correct way of handling it. They are provided always, so throwing them away and then asking for nearly equivalent information with worse accuracy is feeling awkward.
Directly testing the status may look easier for a beginner, but once they are understanding events they are not more difficult. Having to constantly check about 100 keys one by one when most often none is pressed is useless busywork and checking each key at a different time seems also kind of wrong.
Title: Re: Different between sf::Event::KeyPressed and sf::Keyboard::isKeyPressed(...)
Post by: Hapax on February 11, 2014, 05:19:43 pm
That's true for a lot of situations. However, sometimes you need to test what is going on right now, rather than what has happened in the past.
Title: Re: Different between sf::Event::KeyPressed and sf::Keyboard::isKeyPressed(...)
Post by: zsbzsb on February 11, 2014, 05:59:44 pm
Lets be clear on this. sf::Event::KeyPressed is event based and only happens only when a key is pressed. sf::Keyboard::isKeyPressed(...) is realtime based and will let you know what is pressed.

Now each method has pros and cons and should be used when appropriate.

I will try to list of the pros and cons of each method, if anyone has anything else to add then say it. :)

Events
Pros
Cons

Realtime
Pros
Cons
Title: Re: Different between sf::Event::KeyPressed and sf::Keyboard::isKeyPressed(...)
Post by: Hapax on February 11, 2014, 06:29:00 pm
[some good points]
Hear, hear!