100 % reproductible, two programs, the first request to get to the front, the second checks if it lost focus :
Requester, brings window to foreground every 2sec, using windows api
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <windows.h>
int main(){
sf::RenderWindow window(sf::VideoMode(200, 200), "request focus");
sf::Event evt;
while(window.isOpen()){
while (window.pollEvent(evt)){
}
sf::sleep(sf::seconds(2));
SwitchToThisWindow(window.getSystemHandle(),0);
window.clear();
window.display();
}
return 0;
}
Focus checker, displays a green window when it has focus, red if it hasn't
#include <SFML/Graphics.hpp>
int main(){
sf::RenderWindow window(sf::VideoMode(200, 200), "focus checker");
sf::Event evt;
sf::Color c(0,255,0);
while(window.isOpen()){
while (window.pollEvent(evt)){
switch(evt.type){
case sf::Event::LostFocus:
c = sf::Color(255,0,0);
break;
case sf::Event::GainedFocus:
c = sf::Color(0,255,0);
break;
}
}
window.clear(c);
window.display();
}
return 0;
}
Run both programs, you'll see the focus checker stays green when the requester gets to the foreground. No problem when the user switch window or click outside, it seems to happen only when another window takes the focus by itself.
See compiled test in attachment. It doesn't check for window close event so you have to kill them using task manager