SFML community forums

Help => System => Topic started by: romhayh on May 25, 2020, 01:20:15 pm

Title: sf::Event::keyReleased dosent respond
Post by: romhayh on May 25, 2020, 01:20:15 pm
hey there,

as the title says, the keyReleased doesnt respond:

extern sf::RenderWindow* window;
#define SPACE sf::Keyboard::Key::Space

void gameplay::pause()
{

        //pausing by space;
        sf::Event event;
        while (window->pollEvent(event)) {
                if (event.type == sf::Event::KeyReleased) {
                        if (event.key.code == SPACE) {
                                sf::Clock clock;
                                while (clock.getElapsedTime().asSeconds() <= 1.5f) {}
                                bool goon{ true };
                                while (goon) {
                                        while (window->pollEvent(event)) {
                                                if (event.type == sf::Event::KeyReleased) {
                                                        if (event.key.code == SPACE) {
                                                                goon = false;
                                                        }
                                                }
                                        }
                                }
                        }
                }
        }
       

}

 
Title: Re: sf::Event::keyReleased dosent respond
Post by: Athenian Hoplite on May 25, 2020, 02:38:36 pm
What is this code doing ? You check if the space was released and then if it is you check again if the space is released ? Really you should simply catch sf::Event::KeyReleased and toggle your pause variable/method. Also note that if this isn't the only place you are pumping the window then you might be loosing events. Besides the way that your implementing this will also cause loss of any events that happen while you wait for space to be released again which might be fine since the game is paused but then again it might not.
Title: Re: sf::Event::keyReleased dosent respond
Post by: romhayh on May 25, 2020, 03:08:07 pm
What is this code doing ? You check if the space was released and then if it is you check again if the space is released ?

to pause the game, i am checking if the space bar is released, and if it is, i will wait a bit more than a second and then wait until the user decides to end the pause by pressing space
Title: Re: sf::Event::keyReleased dosent respond
Post by: Athenian Hoplite on May 25, 2020, 03:31:12 pm
Key events have a delay already so you don't need to wait to prevent multiple events from a single click (if that is why you are using the delay). You can simply catch a sf::Event::KeyReleased and toggle your pause variable since the way you are doing it ANY events during pause get no answer.

Regardless of this the main point was: is this the only place you are looping window events ? If not you are loosing them somewhere else.

bool paused = false;
sf::Event event;
while (window->pollEvent(event))
{
        if (event.type == sf::Event::KeyReleased)
        {
                if (event.key.code == SPACE)
                {
                          paused = ! paused;
                }
         }
}
 

This code will work if this is the only place you loop window events. How exactly you would then use the "paused" variable is actually another matter.
Title: Re: sf::Event::keyReleased dosent respond
Post by: romhayh on May 25, 2020, 06:14:17 pm

Regardless of this the main point was: is this the only place you are looping window events ? If not you are loosing them somewhere else.


i have another event loop at the main menu.
 i have implemented what's you said at the last comment and now the code is working just fine -
i had put my pause function in the main's event loop;
Title: Re: sf::Event::keyReleased dosent respond
Post by: Hapax on May 26, 2020, 06:51:23 am
Firstly, I'd like to agree with Athenian Hoplite's suggestion: use a pause variable and update/don't update your game based on this variable.

Secondly, (even though you're not using the original code idea) it looks like the reason it didn't "respond" could be that the key event is probably caught before sending the program to this method, which then checks to see if it was pressed(released) again - twice.