-
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.
-
You need to show a complete and minimal example, because the problem probably isn't in the posted code, but in the surrounding code....
-
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?
-
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... ;)
-
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!
-
That piece of code works perfectly on my machine with the latest source.
What is your OS version and Mac model ?
-
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!
-
Yes, try to compile the last source. Maybe this was fixed since the RC (but I don't remember doing it...)
-
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?
-
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;
}
-
This is the expected behaviour with real time input. If you have long processing, use sf::Event instead.
-
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.
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.
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.
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 :)
-
I would deffinitely go with events, key pressed is only for doing stuff like arrow keys to move a character around
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