The window doesn't seem to receive a GainedFocus event if the window is recreated after losing focus.
Here's an example:
#include <SFML/Graphics.hpp>
void createWindow(sf::RenderWindow& window)
{
window.create(sf::VideoMode(800, 600), "Gained Focus Event Failing", sf::Style::Default);
}
int main()
{
sf::RenderWindow window;
createWindow(window);
sf::Color color{ sf::Color::Green };
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed || event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape)
window.close();
else if (event.type == sf::Event::LostFocus)
{
createWindow(window); // this stops GainedFocus from working
color = sf::Color::Red;
}
else if (event.type == sf::Event::GainedFocus)
{
//createWindow(window); // this is fine if commented out or not
color = sf::Color::Green;
}
}
window.clear(color);
window.display();
}
return EXIT_SUCCESS;
}
The code changes the window to red when it's not in focus and green when it is in focus.
When the window is recreated after losing focus, when returning focus to the window, it doesn't receive the GainedFocus event and therefore no longer turns green.
In the code, if you comment out the
createWindow line (before turning color to red), the event works fine. In addition, if you uncomment the
createWindow line (before turning color to green), it makes no difference (lost focus still works immediately). In fact, even after regaining focus but not receiving the GainedFocus event, the window will still receive LostFocus events.
It's obvious that
event is still for the correct window as you can still close the window that can't regain focus.
EDIT: Win7, VS2013, SFML2.3 debug and release (32-bit).
EDIT 2: modified thread subject title.