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

Author Topic: sf::Event::keyReleased dosent respond  (Read 9880 times)

0 Members and 1 Guest are viewing this topic.

romhayh

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
sf::Event::keyReleased dosent respond
« 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;
                                                        }
                                                }
                                        }
                                }
                        }
                }
        }
       

}

 

Athenian Hoplite

  • Newbie
  • *
  • Posts: 19
  • Nenikikamen !
    • View Profile
Re: sf::Event::keyReleased dosent respond
« Reply #1 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.
« Last Edit: May 25, 2020, 03:37:40 pm by Athenian Hoplite »
"By will of the Athenian People be it resolved:
If anyone rises up against the people for tyranny or join in establishing the tyranny or overthrow the People of the Athenians and the democracy in Athens, whoever kills him who does these things shall be blameless." - Athenian Law 337 BC

romhayh

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: sf::Event::keyReleased dosent respond
« Reply #2 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

Athenian Hoplite

  • Newbie
  • *
  • Posts: 19
  • Nenikikamen !
    • View Profile
Re: sf::Event::keyReleased dosent respond
« Reply #3 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.
« Last Edit: May 25, 2020, 03:33:50 pm by Athenian Hoplite »
"By will of the Athenian People be it resolved:
If anyone rises up against the people for tyranny or join in establishing the tyranny or overthrow the People of the Athenians and the democracy in Athens, whoever kills him who does these things shall be blameless." - Athenian Law 337 BC

romhayh

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: sf::Event::keyReleased dosent respond
« Reply #4 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;

Hapax

  • Hero Member
  • *****
  • Posts: 3350
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: sf::Event::keyReleased dosent respond
« Reply #5 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.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything