SFML community forums

Help => Window => Topic started by: Made Y on May 02, 2017, 06:28:42 pm

Title: [Help] counting 1 Pressed keys when the window is Lost focus
Post by: Made Y on May 02, 2017, 06:28:42 pm
i have tried this insde the pollEvent loop

window.setKeyRepeatEnabled(false);
          .
          .
          .
 if (e.type == Event::KeyPressed && e.key.code == Keyboard::Z && e.type == Event::LostFocus){
   count.addkey1(); //key++
}

but that didnt count the pressed keys when the window is lost focus,
and i tried ths outside the pollEvent loop

window.setKeyRepeatEnabled(false);
          .
          .
          .
if(Keyboard::isKeyPressed(Keyboard::Z)){
   count.addkey1(); //key++
}
and it works, can still countin when the window is lost focus. but it count repeatedly when it is pressed.

i want 1 pressed key = 1count, even though the window is lost focus.
im using sfml 2.0.
sorry for my bad english. please help and thankyou.
Title: Re: [Help] counting 1 Pressed keys when the window is Lost focus
Post by: Laurent on May 02, 2017, 07:56:15 pm
Quote
if (e.type == Event::KeyPressed && e.key.code == Keyboard::Z && e.type == Event::LostFocus){
Do you really think the variable e.type can have both Event::KeyPressed and Event::LostFocus values at the same time? ;)

If you want to detect a key pressed while the window doesn't have the focus:
if (e.type == Event::KeyPressed && e.key.code == Keyboard::Z && !window.hasFocus()){
Title: Re: [Help] counting 1 Pressed keys when the window is Lost focus
Post by: Made Y on May 03, 2017, 10:13:17 am
i have installed sfml2.3.2 and tried using !window.hasFocus().
but its still didnt count when the window is lost focus.

and heres the main: 

int main(){
   kps count;
   RenderWindow kps_wind(VideoMode(275,75),"KeyPerSecond");
   while(kps_wind.isOpen()){
      kps_wind.setKeyRepeatEnabled(false);
      Event e;
      while(kps_wind.pollEvent(e)){
         if(e.type == Event::Closed)
            kps_wind.close();
         
         if ((e.type == Event::KeyPressed && e.key.code == Keyboard::Z) && !kps_wind.hasFocus()){
            count.addkey1();
         }else if((e.type == Event::KeyPressed && e.key.code == Keyboard::Z) && kps_wind.hasFocus()){
            count.addkey1();
         }
      }

      //////DRAW//////
      Font font;
      if (!font.loadFromFile("Arial.ttf")){}
      //Text text(count.getkey1(), font, 24);
      Text text;
         text.setString(count.getkey1());
         text.setFont(font);
         text.setCharacterSize(24);
         text.setColor(Color::Red);
         text.setPosition(0,0);

      kps_wind.clear(Color::Black);
      kps_wind.draw(text);
      kps_wind.display();

   }
   return 0;   
}

please help  :(
Title: Re: [Help] counting 1 Pressed keys when the window is Lost focus
Post by: eXpl0it3r on May 03, 2017, 12:42:15 pm


If you want to detect a key pressed while the window doesn't have the focus:
if (e.type == Event::KeyPressed && e.key.code == Keyboard::Z && !window.hasFocus()){
Except, events won't trigger if the window doesn't have the focus.
You can use sf::Keyboard::isKeyPressed() to check the key state, but I wonder what you're trying to do that you want to process keyboard inputs when your window doesn't have focus.
Title: Re: [Help] counting 1 Pressed keys when the window is Lost focus
Post by: Made Y on May 03, 2017, 01:16:26 pm
i want to create my own application to count key per second. so when i played some rhythm games. i can get to know how many keys that i type and how fast can i type. and i need the window to get every keys that i type even when the window is lost focus.

I hv tried using this outside pollEvent loop

window.setKeyRepeatEnabled(false);
          .
          .
          .
if(Keyboard::isKeyPressed(Keyboard::Z)){
   count.addkey1(); //key++
}

But it count repeatedly.
Is there anyway to do it ? Please help :(
Title: Re: [Help] counting 1 Pressed keys when the window is Lost focus
Post by: eXpl0it3r on May 03, 2017, 03:10:12 pm
You'd better off by using the specific OS API or find another library that allows to track events of a different window.

You can "simulate" things by tracking the state of each key and increase the counter when the state changes either from not pressed to pressed or from pressed to not pressed.
Title: Re: [Help] counting 1 Pressed keys when the window is Lost focus
Post by: Made Y on May 03, 2017, 04:26:12 pm
oh. okay, thankyou for the advice sir  ;)