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

Author Topic: Window Focus and Input  (Read 5121 times)

0 Members and 2 Guests are viewing this topic.

aaroncm

  • Newbie
  • *
  • Posts: 20
    • View Profile
Window Focus and Input
« 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

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Window Focus and Input
« Reply #1 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.

mateandmetal

  • Full Member
  • ***
  • Posts: 171
  • The bird is the word
    • View Profile
    • my blog
Re: Window Focus and Input
« Reply #2 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


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
- Mate (beverage) addict
- Heavy metal addict _lml
- SFML 2 addict
- My first (and free) game: BichingISH!

aaroncm

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: Window Focus and Input
« Reply #3 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.

Jebbs

  • Sr. Member
  • ****
  • Posts: 358
  • DSFML Developer
    • View Profile
    • Email
Re: Window Focus and Input
« Reply #4 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?
DSFML - SFML for the D Programming Language.

aaroncm

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: Window Focus and Input
« Reply #5 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..  :)

danikaze

  • Newbie
  • *
  • Posts: 38
    • View Profile
    • Email
Re: Window Focus and Input
« Reply #6 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) ;)