SFML community forums

Help => System => Topic started by: moonfirefly on January 19, 2013, 06:22:04 pm

Title: isKeyPressed does not always capture input
Post by: moonfirefly on January 19, 2013, 06:22:04 pm
I have the following code to check for return key press in my menu main loop :

if (sf::Keyboard::isKeyPressed(sf::Keyboard::Return)) {
    if (m_buttonExit.isSelected()) {
        m_pWindow->close();
    }
    else if (m_buttonStart.isSelected()) {
        newGame();
    }
}

The problem is that sometimes I have to press twice to actually get it to detect the return key press. I don't press any other key at the same time and this code executes at every single loop. I'm on OS X.

I'm not sure what to do at this point. I disabled to key repeat, no difference. The problem is intermittent as well, happens roughly 1 out of 5 times.
Title: Re: isKeyPressed does not always capture input
Post by: eXpl0it3r on January 19, 2013, 06:27:08 pm
You need to show a complete and minimal example, because the problem probably isn't in the posted code, but in the surrounding code....
Title: Re: isKeyPressed does not always capture input
Post by: moonfirefly on January 19, 2013, 07:27:48 pm
See below a complete small example that reproduces the problem:

#include <SFML/Graphics.hpp>

int main (int argc, const char * argv[]) {
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");        

    while (window.isOpen()) {
        sf::Event event;
        while (window.pollEvent(event)) {
                if (event.type == sf::Event::Closed) {
                        window.close();
            }
        }

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Return)) {
            window.close();
        }
       
        window.clear();
        window.display();
    }

        return EXIT_SUCCESS;
}

The code closes the window on return key press but sometimes I have to press twice to make it work.
Am I missing something?
Title: Re: isKeyPressed does not always capture input
Post by: eXpl0it3r on January 19, 2013, 08:14:27 pm
Hmm the code should work...

So you said you're using OS X, can you give some more information how you've built SFML and how you've setup things?
I've no experience with OS X, but maybe the main dev for the OS X part will know of a solution... ;)
Title: Re: isKeyPressed does not always capture input
Post by: moonfirefly on January 19, 2013, 09:20:59 pm
Well at least I know my code is fine :)

I use XCode 4.5.2 with the SFML2 project template provided in the SFML install for OSX. I had to do some changes to make it compile though, as indicated in this post : http://en.sfml-dev.org/forums/index.php?topic=10175.0 (http://en.sfml-dev.org/forums/index.php?topic=10175.0)

I'm curious to see if other developers experience the same keyboard input issue on OSX.
Thanks for your input!
Title: Re: isKeyPressed does not always capture input
Post by: Hiura on January 21, 2013, 09:49:58 am
That piece of code works perfectly on my machine with the latest source.

What is your OS version and Mac model ?
Title: Re: isKeyPressed does not always capture input
Post by: moonfirefly on January 21, 2013, 10:00:37 am
OSX 10.8.2 running on a Macbook Pro (June 2012). To this day I still get the issue sometimes, it's annoying. Maybe I should try the nightly builds then. thanks!
Title: Re: isKeyPressed does not always capture input
Post by: Hiura on January 21, 2013, 10:02:26 am
Yes, try to compile the last source. Maybe this was fixed since the RC (but I don't remember doing it...)
Title: Re: isKeyPressed does not always capture input
Post by: Weeve on February 01, 2013, 03:19:39 am
if I read correctly, you used IsKeyPressed, instead of the signal emmitted by SFML, so only if on that millisecond, the key is pressed down, will it return true, this may be your problem?
Title: Re: isKeyPressed does not always capture input
Post by: io on February 01, 2013, 04:56:38 am
By chance, just to rule it out, have you tried a different keyboard?


Going off of Weeve's post, if a sleep (or heavy processing) happens before the if statement that's checking for the window closed, that might be causing it -- but I don't think this would be the case if the minimal code you posted still causes the same results.


sleep used to "simulate" heavy processing which causes me to have to hit enter twice most of the time.
int main()
{

    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");

    while (window.isOpen()) {
        sf::Event event;
        while (window.pollEvent(event)) {
            if (event.type == sf::Event::Closed) {
                window.close();
            }
        }


        _sleep(500);
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Return)) {
            window.close();
        }

        window.clear();
        window.display();
    }

    return EXIT_SUCCESS;
}
 
Title: Re: isKeyPressed does not always capture input
Post by: Hiura on February 01, 2013, 07:48:17 am
This is the expected behaviour with real time input. If you have long processing, use sf::Event instead.
Title: Re: isKeyPressed does not always capture input
Post by: moonfirefly on February 01, 2013, 07:53:38 am
Quote
if I read correctly, you used IsKeyPressed, instead of the signal emmitted by SFML, so only if on that millisecond, the key is pressed down, will it return true, this may be your problem?

I changed (https://github.com/moonfirefly/Blokade/blob/master/Blokade/Engine/GFXRuntime.cpp) my code last week to check if the isKeyPressed is true on this frame but it wasn't on the last frame then I process it as "pressed". It is better but I still get the issue. I'm considering using the SFML events, for the menu navigation anyway. For the gameplay itself, isKeyPressed works great.

Quote
By chance, just to rule it out, have you tried a different keyboard?

I have not tried to plug another keyboard on my macbook. Not sure if it works.

Quote
if a sleep (or heavy processing) happens before the if statement that's checking for the window closed, that might be causing it

I read the keyboard very early in the frame main loop, I don't really see a hold up.

Quote
This is the expected behaviour with real time input. If you have long processing, use sf::Event instead.

Maybe SFML events is the way to go then. Wouldn't be that much code to change.

Thanks all for your help :)
Title: Re: isKeyPressed does not always capture input
Post by: Weeve on February 01, 2013, 06:13:18 pm
I would deffinitely go with events, key pressed is only for doing stuff like arrow keys to move a character around

Quote
Quote

    if a sleep (or heavy processing) happens before the if statement that's checking for the window closed, that might be causing it

I read the keyboard very early in the frame main loop, I don't really see a hold up.

Whether the processing is after, or before, in code line number dosn't matter, because the while loop just throws it back to the top, what matters is the time between each of your frames, if its more than 1/20th of a second, then thats your problem  :D