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

Author Topic: isKeyPressed does not always capture input  (Read 8302 times)

0 Members and 1 Guest are viewing this topic.

moonfirefly

  • Newbie
  • *
  • Posts: 28
    • View Profile
isKeyPressed does not always capture input
« 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.
Blokade: Source

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: isKeyPressed does not always capture input
« Reply #1 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....
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

moonfirefly

  • Newbie
  • *
  • Posts: 28
    • View Profile
Re: isKeyPressed does not always capture input
« Reply #2 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?
Blokade: Source

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: isKeyPressed does not always capture input
« Reply #3 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... ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

moonfirefly

  • Newbie
  • *
  • Posts: 28
    • View Profile
Re: isKeyPressed does not always capture input
« Reply #4 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

I'm curious to see if other developers experience the same keyboard input issue on OSX.
Thanks for your input!
Blokade: Source

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: isKeyPressed does not always capture input
« Reply #5 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 ?
SFML / OS X developer

moonfirefly

  • Newbie
  • *
  • Posts: 28
    • View Profile
Re: isKeyPressed does not always capture input
« Reply #6 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!
Blokade: Source

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: isKeyPressed does not always capture input
« Reply #7 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...)
SFML / OS X developer

Weeve

  • Jr. Member
  • **
  • Posts: 87
  • C++ Programmer (Intermediate), 3D Artist (Skilled)
    • View Profile
    • Email
Re: isKeyPressed does not always capture input
« Reply #8 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?
Long live rapid project development! -- Kestrel3D Game-Engine nearing completion

io

  • Jr. Member
  • **
  • Posts: 52
  • z/OS by day, SFML by night
    • View Profile
Re: isKeyPressed does not always capture input
« Reply #9 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;
}
 
« Last Edit: February 01, 2013, 06:37:46 am by io »

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: isKeyPressed does not always capture input
« Reply #10 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.
SFML / OS X developer

moonfirefly

  • Newbie
  • *
  • Posts: 28
    • View Profile
Re: isKeyPressed does not always capture input
« Reply #11 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 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 :)
Blokade: Source

Weeve

  • Jr. Member
  • **
  • Posts: 87
  • C++ Programmer (Intermediate), 3D Artist (Skilled)
    • View Profile
    • Email
Re: isKeyPressed does not always capture input
« Reply #12 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
Long live rapid project development! -- Kestrel3D Game-Engine nearing completion

 

anything