SFML community forums

Help => Window => Topic started by: bobingabout on March 02, 2014, 10:24:11 pm

Title: sf::Keyboard::isKeyPressed only when window is active?
Post by: bobingabout on March 02, 2014, 10:24:11 pm
As anyone who needs to use the keyboard will know, there are 2 methods for grabbing keyboard input in SFML.

Code: [Select]
if(sf::Keyboard::isKeyPressed(sf::Keyboard::A))and
Code: [Select]
if((Event.type == sf::Event::KeyPressed) && (Event.key.code == sf::Keyboard::A))
The first of which is true whenever the A key is held down, and ALWAYS when the A key is held down, wether the window is active or not.

The second, is a more classic "Key pressed" function, and returns true the moment A is initially pressed (and repeats if held due to OS key auto-repeat when held funtion), but since this is checked against an event returned by sf::Window::pollEvent(sf::Event) is only true if the window is currently active when the key is pressed.

I had a look at the sf::Window class to see if there was a function return to check if the window was currently active or not, but couldn't see anything...

Does anyone have a solution (Looking for something to add to the if() statement here) to make my first if check true ONLY when the window is currently active?

I was looking to do something along the lines of
Code: [Select]
if(window.isActive() && sf::Keyboard::isKeyPressed(sf::Keyboard::A))but as mentioned, the aparant lack of an isActive() function on sf::Window makes that difficult.
Title: Re: sf::Keyboard::isKeyPressed only when window is active?
Post by: zsbzsb on March 02, 2014, 10:26:49 pm
Handle window got/lost focus events and set a boolean flag accordingly if the window has focus.  ;)
Title: Re: sf::Keyboard::isKeyPressed only when window is active?
Post by: bobingabout on March 02, 2014, 10:48:01 pm
Code: [Select]
if(Event.type == sf::Event::GainedFocus) focus = true;and
Code: [Select]
if(Event.type == sf::Event::LostFocus) focus = false;?
Title: Re: sf::Keyboard::isKeyPressed only when window is active?
Post by: zsbzsb on March 02, 2014, 10:51:59 pm
Yes  :)
Title: Re: sf::Keyboard::isKeyPressed only when window is active?
Post by: bobingabout on March 02, 2014, 11:06:53 pm
Thankyou, trying that now, it seems to do the job.
Title: Re: sf::Keyboard::isKeyPressed only when window is active?
Post by: Hapax on March 03, 2014, 12:25:22 am
...to see if there was a function return to check if the window was currently active or not, but couldn't see anything...
...
Does anyone have a solution
...
I was looking to do something along the lines of
Code: [Select]
if(window.isActive() && sf::Keyboard::isKeyPressed(sf::Keyboard::A))
I do what zsbzsb mentioned - keeping track of focus events with a boolean; it works well enough. However, applications occasionally start without focus so it needs to lose it and regain it for your boolean to be correct. I believe there is a ticket for this fix on SFML.

You didn't mention which operating system you are using but there is a temporary solution for Windows. You'll need to #include <Windows.h> and use this line:
SetForegroundWindow(window.getSystemHandle());
after creating the window with SFML. window is the window that was created.
This should also send a sf::Event::GainedFocus event to the application.