SFML community forums

Help => Window => Topic started by: tortugapower on January 17, 2022, 08:34:27 pm

Title: Keyboard Input : event::keypressed vs. keyboard::isKeyPressed
Post by: tortugapower on January 17, 2022, 08:34:27 pm
[edit]
Too hasty a post, as I have found this chain:
https://en.sfml-dev.org/forums/index.php?topic=14350.0 (https://en.sfml-dev.org/forums/index.php?topic=14350.0)

To which zsbzsb's answer has given me something to chew on.

Cheers.



[original]
Hello. I have seen implementations for keyboard input done two ways:
1) while polling events, check event.type for event::keypressed
2) directly query keyboard::isKeyPressed(key)

I saw an SFML dev reference that either one could be used for, e.g., a player entity's movement (so long as the event method sets a boolean input state rather than enact movement directly, in order to avoid the initial stutter).

I have seen both of these work on my own (including correcting for the stutter). My question: is there a difference? It could be subtle or obvious, but I'd be happy to know more.

Thank you.
Title: Re: Keyboard Input : event::keypressed vs. keyboard::isKeyPressed
Post by: eXpl0it3r on January 18, 2022, 07:06:42 am
For a lot of cases, it doesn't really matter what you pick, either way is good enough.
Title: Re: Keyboard Input : event::keypressed vs. keyboard::isKeyPressed
Post by: kojack on January 18, 2022, 08:33:09 am
One big benefit to the event style is for order dependent processing of keys, such as typing a name. You don't want to have to manually do an isKeyPressed for every possible key, instead the event will tell you which key was pressed so you can add it to a string.

Although the direct style is what I prefer for game related controls, since for those you need to track the state of a key. An event is fine to start a player jump, but to run to the right when you hold a key, that needs to track the presses and releases to know the current state if you used events.

I use my own wrapper on top of sfml's events that lets me query if a key (or mouse button or xbox controller button) was pressed since last update, released since last update or current held down.