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

Author Topic: How to find out if window has focus?  (Read 15390 times)

0 Members and 1 Guest are viewing this topic.

sharethis

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
How to find out if window has focus?
« on: April 28, 2013, 06:09:22 pm »
I use the debugging tools of Visual Studio. Therefore my application is run from within the Visual Studio task. Whenever the window of Visual Studio is in background thus hasn't the focus, the window of my application has no focus neither. I guess this is typical behavior of Windows.

Therefore the window of my application sometimes opens with focus and otherwise without. Normally I would track focus changes by sf::Events but since it's created in that state, there is no focus change at all. So I have no chance to check if the window has or hasn't focus, have I?

There should be a function in SFML for fetching the focus state of a window at any time!

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
AW: How to find out if window has focus?
« Reply #1 on: April 28, 2013, 06:21:04 pm »
If the event don't get triggeted, there's nothing you can do (with SFML) and it's also not a problem of SFML but of Visual Studio...
Can you provide a minumal example, so we could test it as well?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

sharethis

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
Re: How to find out if window has focus?
« Reply #2 on: April 28, 2013, 06:30:18 pm »
Just run any task from within a window in background, it's window will be in background, too. There's nothing special about Visual Studio. It won't make sense to provide a minimal example. If you need one, just use the minimal code to open a window in SFML and run the code from within a task with it's window not focused. I copied the following line from the window tutorial.

sf::Window App(sf::VideoMode(800, 600, 32), "SFML Window");

It's just that a SFML window, like any other window, isn't guaranteed to be opened with focus. Currently in SFML there is no way to keep track of the focus state from the beginning. Only after the focus state changed the first time, the programmer can keep track.

Therefore a bool sf::Window::IsFocused() function would be very useful.

fstream

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: How to find out if window has focus?
« Reply #3 on: May 02, 2013, 02:11:25 pm »
Currently in SFML there is no way to keep track of the focus state from the beginning.
I just tested it on Linux, and I get a GainedFocus event at the very start of the program - do you not on Windows?

I'd assume if the window were to open without getting focus, a GainedFocus event will not be created.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: How to find out if window has focus?
« Reply #4 on: May 02, 2013, 02:45:05 pm »
Now that I'm not on my phone, I've tested it, but I can't reproduce your problem.

#include <SFML/Graphics.hpp>
#include <iostream>

int main()
{
        sf::RenderWindow window(sf::VideoMode(800, 600), "Hello");

        while(window.isOpen())
        {
                sf::Event event;
                while(window.pollEvent(event))
                {
                        if(event.type == sf::Event::Closed)
                                window.close();
                        else if(event.type == sf::Event::GainedFocus)
                                std::cout << "Gained" << std::endl;
                        else if(event.type == sf::Event::LostFocus)
                                std::cout << "Lost" << std::endl;
                }

                window.clear();
                window.display();
        }
}

It yields always the lost event if the window doesn't have the focus. So when you create it, you should be able to assume that it starts with the focus on the window and when it's lost, you'll get the lost event.
But maybe I didn't understand you clearly enough. In which situation do you don't have the focus, but also haven't received a lost event?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

novemberdobby

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • http://www.novemberdobby.co.uk
Re: How to find out if window has focus?
« Reply #5 on: May 09, 2013, 10:35:34 pm »
I had similar problems, so I added a function for testing focus directly.

Here's the Windows implementation:

bool appInFocus(sf::RenderWindow* app)
{
        if(app == NULL)
                return false;

        HWND handle = app->getSystemHandle();
        bool one = handle == GetFocus();
        bool two = handle == GetForegroundWindow();

        if(one != two) //strange 'half-focus': window is in front but you can't click anything - so we fix it
        {
                SetFocus(handle);
                SetForegroundWindow(handle);
        }

        return one && two;
}
 

This has always worked perfectly for me.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: How to find out if window has focus?
« Reply #6 on: May 09, 2013, 11:37:48 pm »
You probably should not mix a getter function with side effects ("so we fix it") -- then you could also declare the parameter const without guilty conscience ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

sharethis

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
Re: How to find out if window has focus?
« Reply #7 on: May 11, 2013, 05:41:07 pm »
I just tested it on Linux, and I get a GainedFocus event at the very start of the program - do you not on Windows?

I'd assume if the window were to open without getting focus, a GainedFocus event will not be created.
I double checked that. On my Windows 8 machine, there is never a gained event at startup.

It yields always the lost event if the window doesn't have the focus. So when you create it, you should be able to assume that it starts with the focus on the window and when it's lost, you'll get the lost event.
But maybe I didn't understand you clearly enough. In which situation do you don't have the focus, but also haven't received a lost event?
It's when the window is created in background. The operating system decides whether that happens. Of course there is no lost event since the window never had focus before.

I had similar problems, so I added a function for testing focus directly.
Thanks a lot. Sadly that's not cross platform. I'll use that code, but my feature request to implement that for all platforms in SFML remains.

Grimshaw

  • Hero Member
  • *****
  • Posts: 631
  • Nephilim SDK
    • View Profile
Re: How to find out if window has focus?
« Reply #8 on: May 12, 2013, 03:41:53 pm »
This is indeed a real problem, I would also like to see these improvements in SFML. Right now its a little painful to make a FPS game or something.

novemberdobby

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • http://www.novemberdobby.co.uk
Re: How to find out if window has focus?
« Reply #9 on: May 14, 2013, 12:11:27 am »
Thanks a lot. Sadly that's not cross platform. I'll use that code, but my feature request to implement that for all platforms in SFML remains.

Luckily I had to also do this in Linux and OSX ;) it's really hacky, but let me know if you'd like the code.

Grimshaw

  • Hero Member
  • *****
  • Posts: 631
  • Nephilim SDK
    • View Profile
Re: How to find out if window has focus?
« Reply #10 on: May 14, 2013, 04:25:11 am »
I would like to look at that code if possible! Thanks :)

sharethis

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
Re: How to find out if window has focus?
« Reply #11 on: August 07, 2013, 02:20:08 pm »
Is this going to be integrated in SFML? I think novemberdobby has cross plattform code for this.