SFML community forums

Help => Window => Topic started by: Léandre on July 26, 2017, 02:23:00 pm

Title: sf::RenderWindow responds to clicks, on background ...
Post by: Léandre on July 26, 2017, 02:23:00 pm
Hi,

I'm not really new with sfml, but was using CSFML, well now I'm learning C++ and doin pretty well,
So I'm creating a tiny game, where I need to handle mouse position, my main problem is that, even if I'm focused
in a Terminal of other software, the window still responds in background, so I can click on the sfml window, while being in my C++ code for example.
Hum, is there any way to avoid this ? Because I'm using isKeyPressed and isMousePressed so maybe witch the events ?

Best regards,
Léandre
Title: Re: sf::RenderWindow responds to clicks, on background ...
Post by: Léandre on July 26, 2017, 02:25:07 pm
Nevermind, I've found the LostFocus event
Title: Re: sf::RenderWindow responds to clicks, on background ...
Post by: Hapax on July 27, 2017, 11:33:37 pm
LostFocus and GainedFocus can solve your problem but you will have to track the focus state to know whether it's currently in focus or not (it would only need a simple boolean, of course).

However, and especially since you're using real-time states, you may also be interested in hasFocus (https://www.sfml-dev.org/documentation/2.4.2/classsf_1_1Window.php#ac4dce670f07c5039a732ba0903ce3a77), which can inform you of the current state of focus.

e.g.
if (window.hasFocus())
{
    if (isKeyPressed(sf::Keyboard::Up))
        moveUp();
    if (isKeyPressed(sf::Keyboard::Down))
        moveDown();
    // etc.
}