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

Author Topic: Keyboard Input : event::keypressed vs. keyboard::isKeyPressed  (Read 3498 times)

0 Members and 1 Guest are viewing this topic.

tortugapower

  • Newbie
  • *
  • Posts: 1
    • View Profile
Keyboard Input : event::keypressed vs. keyboard::isKeyPressed
« 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

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.
« Last Edit: January 17, 2022, 08:51:43 pm by tortugapower »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Re: Keyboard Input : event::keypressed vs. keyboard::isKeyPressed
« Reply #1 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: Keyboard Input : event::keypressed vs. keyboard::isKeyPressed
« Reply #2 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.

 

anything