-1

Trying to figure out how to close a window using a button, in this case the 'Esc'-button. In my first approach I tried going like this:

if (sf::event.type == sf::Keyboard::Escape)
    window.close();

That didn't turn out very well, doesn't look like my program notices me pressing the Escape button. So I tried this approuch:

if (sf::event.type == sf::Keyboard::isKeyPressed(sf::Keyboard::Esc))
    window.close();

That didn't work either, and just before I was about to ask here I figured out that this line actually works (thou it doesn't feel right since I am actually handling events?)

if (sf::Keyboard::isKeyPressed(sf::Keyboard::Esc))
    window.close();

My question is; what is the right way to do it? And why doesn't if work writing like I did first? It seems that' according to YouTube-videos the first and second approach works. But in SFML 2.0 and forward, only the last one does. Is that the case?

3

4 Answers 4

4

The first two do not work, because you're just guessing. The check consists of checking the event type, and then checking the key code:

if (event.type == sf::Event::KeyPressed)
    if (event.key.code == sf::Keyboard::Escape)
        window.close();

Reference

The second random code you wrote:

sf::event.type == sf::Keyboard::isKeyPressed(sf::Keyboard::Esc)

sf::Keyboard::isKeyPressed returns bool and sf::event.type is not 1 i.e. sf::Event::Resized at that point (unless you hold the key and resize the window).

To the last code sample:

if (sf::Keyboard::isKeyPressed(sf::Keyboard::Esc))
    window.close();

it is also a viable way. If put it under the condition of sf::Window::pollEvent being successful, it will close your window anyway. But it's usually outside, and sf::Keyboard makes it easier to implement character movement in games.

2

As LogicStuff mentioned earlier, just do this-:

if(window.event.type == sf::Event::KeyPressed)
    if(event.key.code == sf::Keyboard::Escape)
        window.close();

SFML follows this approach so that events of similar types are grouped together. It may appear to be something unintuitive at first but its there so that the readability of the code increases. And always check out the official docs. They're really good.(Atleast in comparison to some of the tools that I have to use.:))

2
  • window.event? I don't think repeating my answer was necessary.
    – LogicStuff
    Feb 14, 2016 at 13:15
  • @LogicStuff: sorry for making my answer look like a copy. I just wanted to tell him why all this is the way it is....you see most of my answer isn't code....its about telling him the possible rationale behind such a design.
    – NightMare
    Feb 14, 2016 at 17:21
1

This works for me:

sf::Event event;
while(window.pollEvent(event))
{
    if(event.type == sf::Event::Closed)
        window.close();
    else if(event.type == sf::Event::KeyPressed)
    {
        if(event.key.code == sf::Keyboard::Key::Escape)
            window.close();
        // ... etc ...
    }
}
0

Assuming all of the SFML is set-up correctly, you can add the following (almost) anywhere in the loop:

if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) { window.close(); }

1
  • 1
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Jun 13, 2022 at 20:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.