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

Author Topic: How to tell if no key is pressed?  (Read 3986 times)

0 Members and 1 Guest are viewing this topic.

mos

  • Newbie
  • *
  • Posts: 19
    • View Profile
How to tell if no key is pressed?
« on: September 03, 2016, 03:47:01 am »
I looked in the documentation (http://www.sfml-dev.org/documentation/2.4.0/classsf_1_1Keyboard.php) but can't seem to find anything for real time keyboard input that handles if any key is pressed or not. I am trying to make a sprite move and reset its frame in the spritesheet when nothing is pressed.

I know I can do this:

if (!sf::Keyboard::isKeyPressed(sf::Keyboard::Up) && (!sf::Keyboard::isKeyPressed(sf::Keyboard::Down) && etc...)
{
    // Reset stuff
}

However, this will take a long time depending on how many actions there are for the sprite. Is there a faster way?

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: How to tell if no key is pressed?
« Reply #1 on: September 03, 2016, 10:53:44 am »
If it's acceptable to start the app with a key down and ignore it, then count the # of key pressed and key released event and compare them.

Otherwise, do a loop on the sf::Keyboard values (yes, you can do that, they are just named numbers).
SFML / OS X developer

Hapax

  • Hero Member
  • *****
  • Posts: 3353
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: How to tell if no key is pressed?
« Reply #2 on: September 03, 2016, 08:48:02 pm »
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

mos

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: How to tell if no key is pressed?
« Reply #3 on: September 05, 2016, 03:36:32 am »
Thank you so much both of you! The loop works great.