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

Author Topic: sf::Keyboard::isKeyPressed only when window is active?  (Read 6095 times)

0 Members and 1 Guest are viewing this topic.

bobingabout

  • Newbie
  • *
  • Posts: 47
    • View Profile
sf::Keyboard::isKeyPressed only when window is active?
« 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.
« Last Edit: March 02, 2014, 10:49:15 pm by bobingabout »

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: sf::Keyboard::isKeyPressed only when window is active?
« Reply #1 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.  ;)
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

bobingabout

  • Newbie
  • *
  • Posts: 47
    • View Profile
Re: sf::Keyboard::isKeyPressed only when window is active?
« Reply #2 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;?

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: sf::Keyboard::isKeyPressed only when window is active?
« Reply #3 on: March 02, 2014, 10:51:59 pm »
Yes  :)
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

bobingabout

  • Newbie
  • *
  • Posts: 47
    • View Profile
Re: sf::Keyboard::isKeyPressed only when window is active?
« Reply #4 on: March 02, 2014, 11:06:53 pm »
Thankyou, trying that now, it seems to do the job.

Hapax

  • Hero Member
  • *****
  • Posts: 3349
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: sf::Keyboard::isKeyPressed only when window is active?
« Reply #5 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.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything