SFML community forums

Help => General => Topic started by: aaroncm on December 02, 2012, 01:53:09 am

Title: Window Focus and Input
Post by: aaroncm on December 02, 2012, 01:53:09 am
Hey guys. I've been reading a little on the forum about this problem, seems others have had it =)

I've been using events for things like text input, but not much else, I've stuck to the isButtonPressed() functions for mouse click, and I didn't realize that this function returns true even when the window is out of focus. I didn't really want to pull all my code apart to fix it, I was hoping that I've missed something,  maybe some mode I could set to disable input when the window is out of focus? I hope :(

Any suggestions?

Thanks guys
Title: Re: Window Focus and Input
Post by: G. on December 02, 2012, 02:41:00 am
If you want to know whether your window has the focus or not, track the GainedFocus and LostFocus events.
Title: Re: Window Focus and Input
Post by: mateandmetal on December 03, 2012, 06:36:23 am
I was hoping that I've missed something..

Exactly


Quote
..you always get the real state of the mouse, even if it is moved, pressed or released when your window is out of focus and no event is triggered.

sf::Mouse (http://www.sfml-dev.org/documentation/2.0/classsf_1_1Mouse.php#ab647159eb88e369a0332a9c5a7ba6687)


Why donĀ“t you handle the mouse events?

if (event.type == sf::Event::MouseButtonPressed)
{
    if (event.mouseButton.button == sf::Mouse::Right)
    {
        std::cout << "the right button was pressed" << std::endl;
        std::cout << "mouse x: " << event.mouseButton.x << std::endl;
        std::cout << "mouse y: " << event.mouseButton.y << std::endl;
    }
}
 

Code from the Events tutorial (http://www.sfml-dev.org/tutorials/2.0/window-events.php)
Title: Re: Window Focus and Input
Post by: aaroncm on December 03, 2012, 06:52:03 am
I've come from an old library called Allegro, and I've never really used event systems like this before in large projects like what I'm working on and I wasn't sure how to implement it.

So far, SFML has been really smooth sailing for me, but when it came to my text input class I ran into some problems where if I used a while(window->pollEvent(event)) in the class, and another while(pollEvent) somewhere else, text input would fail to recognize, say, 80% of keystrokes, so I resorted to a while(pollEvent) in the class and just a single pollEvent in the main loop (But now it takes several clicks to close the window) So yeah.
Title: Re: Window Focus and Input
Post by: Jebbs on December 03, 2012, 09:18:45 am
You shouldn't be calling pollEvent from different places in code. That's why you're having those issues. Once an event is handled, it no longer exists in the event queue. Instead, maybe you could pass text based events to your input class as they are handled in your main loop?
Title: Re: Window Focus and Input
Post by: aaroncm on December 04, 2012, 01:52:19 am
Yeah.. I guess I could have some sort of pollData() function or something for my classes that need it, and then do that in a single event loop..  :)
Title: Re: Window Focus and Input
Post by: danikaze on December 04, 2012, 04:06:19 am
I use a personal Input class, not events for keyboard/mouse checks, but I only call Input::update() when the window is on focus.

Check the documentation for the LostFocus and GainedFocus (Window module -> Event) ;)