SFML community forums

Help => Window => Topic started by: Beta_Ravener on August 22, 2013, 12:59:44 am

Title: Windows gains focus only at borded
Post by: Beta_Ravener on August 22, 2013, 12:59:44 am
I've noticed that my windows only gain focus (understand producing sf::Event::GainedFocus and bringing input to that window) only when clicking at it's border, not inside o the window. It's a little annoying and I'm somewhat sure it didn't happen in previous SFML versions (but it's too late for another testing.. ). Other means of gaining focus work fine (alt-tabing, picking up from taskbar,..).

I'm using SFML 2.1 32bit on Windows 8 64bit and can bring up more details if needed, like minimum code to reproduce, however it happens with 2 of my programs using SFML, so it looks like consistent issue.
Title: Re: Windows gains focus only at borded
Post by: eXpl0it3r on August 22, 2013, 01:48:15 am
Yeah noticed that as well and was kinda surprised that nobody had reported it yet, but was too busy/lazy to report it myself... ;D
Title: Re: Windows gains focus only at borded
Post by: Ixrec on August 22, 2013, 02:16:34 am
Actually it's been known for a while: https://github.com/SFML/SFML/issues/437
Title: Re: Windows gains focus only at borded
Post by: xzbobzx on August 24, 2013, 05:38:24 pm
Came here to report this actually, it's bugging.

Is there a workaround?
Edit: I fixed it.

(http://i.imgur.com/zEulzPq.jpg)
Title: Re: Windows gains focus only at borded
Post by: FRex on August 24, 2013, 06:29:05 pm
You misspelled brought as brough. Otherwise perfect fix. ;D
Title: Re: Windows gains focus only at borded
Post by: eXpl0it3r on August 24, 2013, 07:02:38 pm
Edit: I fixed it.
Do you mind sharing what you've done to "fix it"? :)
Title: Re: Windows gains focus only at borded
Post by: G. on August 24, 2013, 07:05:53 pm
Not really a fix, he displays "click border to focus" with a lame rant in the lower right corner. ;)
Title: Re: Windows gains focus only at borded
Post by: xzbobzx on August 24, 2013, 07:08:37 pm
Edit: I fixed it.
Do you mind sharing what you've done to "fix it"? :)
Ah, yes of course.

Basically I've got my event checker looking for the LostFocus event, which switches the 'IsFocused' bool to false. It also switched the gamestate to the pause menu and disables the pause menu's events.
Then later on in the code when I render everything I have my code see if 'IsFocused' is false; if it's false it will display 'Click window border to focus'.
When you regain focus the overlay is removed and it puts the pause menu back in order, from here you're free to continue whatever you were doing.


Not really a fix, he displays "click border to focus" with a lame rant in the lower right corner. ;)

All in good fun  ;)
Title: Re: Windows gains focus only at borded
Post by: FRex on August 24, 2013, 07:15:29 pm
Not really a fix, he displays "click border to focus" with a lame rant in the lower right corner. ;)
Yeah, he should instead move to some 'lame' library that actually has fully working input handling instead of saying the 'lame' truth that a bug was introduced in one of basic pieces of SFML. :-\
Title: Re: Windows gains focus only at borded
Post by: xzbobzx on August 24, 2013, 08:12:07 pm
Not really a fix, he displays "click border to focus" with a lame rant in the lower right corner. ;)
Yeah, he should instead move to some 'lame' library that actually has fully working input handling instead of saying the 'lame' truth that a bug was introduced in one of basic pieces of SFML. :-\
Hey now I love this library as much as the next guy, don't judge me for my bad jokes :(
Title: Re: Windows gains focus only at borded
Post by: xzbobzx on August 24, 2013, 08:28:37 pm
I fixed my fix :>

(http://i.imgur.com/8Nb6uul.jpg)
Title: Re: Windows gains focus only at borded
Post by: Beta_Ravener on August 24, 2013, 09:21:27 pm
Hey, when did reporting SFML bugs become such fun to do?  :D Anyway nice work xzbobzx, in meantime it's going to be probably my choice of 'fix' too. (Might even create fork including this.. or just really fix it.)
Title: Re: Windows gains focus only at borded
Post by: All8Up on August 28, 2013, 06:51:23 am
Just posted a reply to the bug tracker with a very simple way to fix this.  It's not 100% thanks to the message order, i.e. you get a click or drop a click prior to the focus message showing up but for my stuff dropping the click was appropriate and all the other messages show up correctly.
Title: Re: Windows gains focus only at borded
Post by: Mario on August 28, 2013, 10:36:21 am
IMO clicking into a window to gain focus shouldn't trigger a mouse click event anyway, so seems fine to me.
Title: Re: Windows gains focus only at borded
Post by: All8Up on August 28, 2013, 03:59:44 pm
Using a tracking flag seems fine, though the one suggestion I would have is to wrap the check in a helper function such that if more logic needs to be added you don't have to fiddle with each message separately.  Just a trivial sort of rule of thumb type thing.

As to the behavior, I actually ended up putting in a flag on the window to control the behavior in my slightly modified SFML 2.1 because in one place dropping the click was completely appropriate but in another it was extremely annoying.  I'm working on a little secondary helper (debug) tools window for editing some items and not being able to click a button immediately without double clicking in that window was annoying.  Clicking on the viewport windows to activate them before mouse clicks were accepted was perfectly fine though.

I still think it would be possible to simply delay the send of the activation click and post it immediately after WM_SETFOCUS is processed, I just wanted the lightest change possible and figured I'd post it for further discussion.
Title: Re: Windows gains focus only at borded
Post by: Melascry on June 30, 2014, 10:07:32 pm
Just to add my solution.
This is a Windows only solution.

I actually found a coding solution which just gets around this problem. I don't know if it has been solved yet, but it was not working with my actual version for SFML, 2.1, less than a week old.

So to still get the sf::Event::GainedFocus when clicking in the middle of the window, is used an other event. The sf::Event::MouseEntered event.
It gets trigerred whether or not the window is on focus.
So all that is left is to detect the mouseClick Event.
Which is again trigerred even if the window isn't the focus.

Then to make the window the focus. No function from sfml is provided to force the focus on the window. But there is a win32 api call that does the trick. SetFocus(HWND HandleToTheWindow).

case sf::Event::MouseEntered:
{
        // Retrieving when the mouse gets in the window.
        mouseInScreen = true;
        break;
}
case sf::Event::MouseButtonReleased:
{
        if(mouseInScreen)
        {
                // make the window the current focus.
                SetFocus(this->window->getSystemHandle());
        }
}
 

The only downside, if considered one, is that it will send a lose focus event before sending the GainedFocus event.

I hope this helps anyone having this problem again. Or maybe giving the SFML guys some clue about how to get this fixed. :)
Title: Re: Windows gains focus only at borded
Post by: Ixrec on June 30, 2014, 10:26:22 pm
I'm pretty sure that got fixed a while ago: https://github.com/SFML/SFML/pull/457

Also, 2.1 is a lot older than a week.
Title: Re: Windows gains focus only at borded
Post by: Melascry on July 01, 2014, 10:52:43 am
Oh yeah that's not what I meant by a week old ^^'.

I downloaded the 2.1 libs less than a week ago, and that wasn't fix for me :/

Maybe someone can check on their version.
Title: Re: Windows gains focus only at borded
Post by: eXpl0it3r on July 01, 2014, 11:01:22 am
SFML 2.1 doesn't have the fix. You can download the package as often as you want, it just doesn't have it. ;)

If you want a version with the fix, then get the source from GitHub and build SFML yourself. There are no official binaries available as of now.
Title: Re: Windows gains focus only at borded
Post by: Melascry on July 01, 2014, 11:02:41 am
OOOOk that's why ^^

Thanks then, I'll do this ASAP :)
Title: Re: Windows gains focus only at borded
Post by: zzymyn on August 07, 2014, 09:11:47 am
There's a workaround here if you don't/can't build SFML 2.2:

You'll need to #include <CommCtrl.h> and link to Comctl32.lib.

Add this function somewhere:

#if SFML_VERSION_MAJOR == 2 && SFML_VERSION_MINOR == 1
        LRESULT CALLBACK GameWindowHook(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
        {
                if (uMsg == WM_MOUSEMOVE)
                {
                        // Supress mouse move events if the window doesn't have the focus:
                        if (GetActiveWindow() != hWnd)
                                return DefWindowProc(hWnd, uMsg, wParam, lParam);
                }
                return DefSubclassProc(hWnd, uMsg, wParam, lParam);
        }
#endif

And add this after you create your SFML window:

#if SFML_VERSION_MAJOR == 2 && SFML_VERSION_MINOR == 1
        SetWindowSubclass(window.getSystemHandle(), &GameWindowHook, 0, 0);
#endif

I haven't thoroughly tested this or anything so I don't recomment putting it in a release build.
Title: Re: Windows gains focus only at borded
Post by: Hapax on August 07, 2014, 03:19:03 pm
There are a number of hacks like yours around this problem that 2.1 has.

However, if you can't build the latest version from source yourself, there very little reason why you can't download a pre-built version from the Nightly Builds (http://www.nightlybuilds.ch/).
Title: Re: Windows gains focus only at borded
Post by: zzymyn on August 07, 2014, 03:27:04 pm
Thanks Hapax. I didn't see this workaround in the bug thread or via a Google search, so I thought it may be useful to people who Google the bug.

It may sound weird, but I prefer to workaround bugs and stick to the stable versions of libraries rather than use nightlies. Better the devil you know, or something.  :P
Title: Re: Windows gains focus only at borded
Post by: Hapax on August 07, 2014, 03:29:30 pm
Fair enough but the "stable" release is over a year old now so I would say that there are fewer problems in the latest version than that release  ;D
Title: Re: Windows gains focus only at borded
Post by: Gobbles on August 07, 2014, 03:48:49 pm
Also I think your logic is a bit backwards. 'Hacks' require you putting in code to deal with the problem, but you never really know what other systems they might affect, hence why they are a hack. The nightlies are at least tested by a multitude of people before and after they are posted, so they are to a degree stable, unofficially anyways.

Or maybe the SFML Team just throws all the code in there and hopes for the best.. you never know with those guys.
Title: Re: Windows gains focus only at borded
Post by: select_this on August 07, 2014, 03:50:57 pm
Or maybe the SFML Team just throws all the code in there and hopes for the best.. you never know with those guys.

:o
Title: Re: Windows gains focus only at borded
Post by: Jesper Juhl on August 07, 2014, 05:18:30 pm
Or maybe the SFML Team just throws all the code in there and hopes for the best.. you never know with those guys.
I assume (hope) that this comment is intended as a bad joke.
In my experience the SFML team is quite careful about what they commit to the master branch and it is generally quite stable. They most certainly don't just throw everything in there and hope for the best.

I really don't get why people attach so much import to a tagged release - it's just a commit at some semi-random point in the commit history where someone decided that "yeah, seems OK now; let's call this version x.y".  Sure, for some projects it may mean that the release has gone through some formal testing and acceptance criteria. But outside the realm of regulated and ISO certified commercial software I doubt that's the case very often.
In any case, the current tip of the master branch of the SFML library is (IMHO) a lot better than the tagged 2.1 release.
Title: Re: Windows gains focus only at borded
Post by: Gobbles on August 07, 2014, 06:53:17 pm
I assume (hope) that this comment is intended as a bad joke.
In my experience the SFML team is quite careful about what they commit to the master branch and it is generally quite stable. They most certainly don't just throw everything in there and hope for the best.

Yes, it was a joke. Really is a no humor zone around here isn't it.