Skip to main content
added more justifications
Source Link
Lectem
  • 171
  • 6

I would personally use the keyboard class that allows you to query the state of keys instead of waiting for an event.

The documentation is here

One of the reasons I prefer this method is that you can check the status of the key from almost anywhere in the code, and you don't have to store the state of the keys yourself.

f::Keyboard can retrieve the state of a key at any time (you don't need to store and update a boolean on your side in order to know if a key is pressed or released), and you always get the real state of the keyboard, even if keys are pressed or released when your window is out of focus and no event is triggered.

The other obvious advantage is that you can check the state of multiple keys at the same time, hence making it easy to detect key combinations :

if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left) && sf::Keyboard::isKeyPressed(sf::Keyboard::Down))player_.moveDownLeft();

I would personally use the keyboard class that allows you to query the state of keys instead of waiting for an event.

The documentation is here

I would personally use the keyboard class that allows you to query the state of keys instead of waiting for an event.

The documentation is here

One of the reasons I prefer this method is that you can check the status of the key from almost anywhere in the code, and you don't have to store the state of the keys yourself.

f::Keyboard can retrieve the state of a key at any time (you don't need to store and update a boolean on your side in order to know if a key is pressed or released), and you always get the real state of the keyboard, even if keys are pressed or released when your window is out of focus and no event is triggered.

The other obvious advantage is that you can check the state of multiple keys at the same time, hence making it easy to detect key combinations :

if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left) && sf::Keyboard::isKeyPressed(sf::Keyboard::Down))player_.moveDownLeft();
Source Link
Lectem
  • 171
  • 6

I would personally use the keyboard class that allows you to query the state of keys instead of waiting for an event.

The documentation is here