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

Author Topic: One action per isKeyPressed  (Read 6474 times)

0 Members and 1 Guest are viewing this topic.

traxys

  • Newbie
  • *
  • Posts: 12
    • View Profile
    • Email
One action per isKeyPressed
« on: September 28, 2014, 05:43:09 pm »
Hello,

Here is the problem I have , I want to make something move from one lane to another , for that in a loop for window.isOpen() in the pollEvent loop I use this code , wich seemed pretty good to me :
            if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)){
                player.moveLane(UP);
            }
            if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)){
                player.moveLane(DOWN);
            }
 
But the problem here is that on the next occurence of the isOpen loop , it will try again to move , so the player moves twice , while pressing one key. So my idea was too make the player move one lane when pressing the key , how do you do that if this code doesn't work ?

And thhanks to anyone looking here :)
« Last Edit: September 28, 2014, 05:52:54 pm by traxys »

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: One action per isKeyPressed
« Reply #1 on: September 28, 2014, 06:30:29 pm »

dabbertorres

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • website/blog
Re: One action per isKeyPressed
« Reply #2 on: September 28, 2014, 06:47:00 pm »
You can either use events like G suggested (best option imo), or you can disable key repeat with
window.setKeyRepeatEnabled(false);

traxys

  • Newbie
  • *
  • Posts: 12
    • View Profile
    • Email
Re: One action per isKeyPressed
« Reply #3 on: September 28, 2014, 07:15:46 pm »
Thanks guys :)
I has was about the sf::EVenet::KeyPressed but did not understand how it worked , but did not see the tutorial either ><

Hapax

  • Hero Member
  • *****
  • Posts: 3381
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: One action per isKeyPressed
« Reply #4 on: September 28, 2014, 07:44:52 pm »
You can either use events like G suggested (best option imo), or you can disable key repeat with
window.setKeyRepeatEnabled(false);
This stops the events repeating so would be used in conjuction with using events, not instead of using them.

Events (with key-repeating off if desired) does seem the better option, but you can also implement booleans to track whether it's been pressed and released since last time. This is extra work when you have events sat there, waiting for you though  ;)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

AlexAUT

  • Sr. Member
  • ****
  • Posts: 396
    • View Profile
Re: One action per isKeyPressed
« Reply #5 on: September 28, 2014, 08:08:52 pm »
Just in case you want to avoid events for your "realtime" controlls in a game you can save the old state and then check if it has changed, for example:

void checkReturnKey()
{
     static bool lastState = false;
     bool currentState = sf::Keyboard::isKeyPressed(sf::Keyboard::Return);

     if(!lastState && currentState)
          //Got pressed
     else if(lastState && !currentState)
          //Got released      

     lastState = currentState
}
 

With this code "Pressed"/"Released" will only get called 1time like with events.



AlexAUT

wintertime

  • Sr. Member
  • ****
  • Posts: 255
    • View Profile
Re: One action per isKeyPressed
« Reply #6 on: September 29, 2014, 08:19:26 pm »
I would not simulate Events that way, because it still can fail to register short temporary key presses and releases and its a waste of CPU time and programmer effort, when there are Events already produced by SFML.

AlexAUT

  • Sr. Member
  • ****
  • Posts: 396
    • View Profile
Re: One action per isKeyPressed
« Reply #7 on: September 29, 2014, 08:45:31 pm »
when there are Events already produced by SFML.

What if you have an application which doesn't have a window, or the user has to press a key before the window opens (stupid example  ;)).
When the windows is in background this would be the only way to check the keystates (without OS code or changing SFML)


AlexAUT

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: One action per isKeyPressed
« Reply #8 on: September 29, 2014, 08:53:01 pm »
When the windows is in background this would be the only way to check the keystates (without OS code or changing SFML)
This is a very exotic scenario, the main use case that comes to my mind is a keylogger... And indeed I remember my anti-virus triggering an alert when global key input is caught, so it can be wise to deactivate real-time input when the window has no focus.

I agree with wintertime, events should be used for one-time transitions because they're exactly made for that.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

AlexAUT

  • Sr. Member
  • ****
  • Posts: 396
    • View Profile
Re: One action per isKeyPressed
« Reply #9 on: September 29, 2014, 09:22:18 pm »
This is a very exotic scenario, the main use case that comes to my mind is a keylogger...

Exotic scenario when the window is in background? Skype, Teamspeak have to check keystates when the window is in background (Push to talk, hotkeys), MSI afterburner to enable the overlay ingame, nearly every screen/game capturing application to start the capture, OBS/XSplit to change scenes, so mainly it's useful for hotkeys but i do not think they are exotic for mutlimedia applications ;)

AlexAUT

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: One action per isKeyPressed
« Reply #10 on: September 29, 2014, 09:33:32 pm »
True, good examples. These applications however have in common that they require more rights than typical games (access to microphone, camera, other applications, current desktop screen, or... global keyboard input).

Your first post sounds like it would be a common approach to detect transitions between real-time input instead of using events directly, and given the initial problem description, I think it's very unlikely that this is actually needed. I just found the comment rather confusing without a notice of actual use cases, but it should be clear after your clarification ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything