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

Author Topic: [Help] counting 1 Pressed keys when the window is Lost focus  (Read 2404 times)

0 Members and 1 Guest are viewing this topic.

Made Y

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: [Help] counting 1 Pressed keys when the window is Lost focus
« Reply #1 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()){
Laurent Gomila - SFML developer

Made Y

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: [Help] counting 1 Pressed keys when the window is Lost focus
« Reply #2 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  :(

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10820
    • View Profile
    • development blog
    • Email
Re: [Help] counting 1 Pressed keys when the window is Lost focus
« Reply #3 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Made Y

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: [Help] counting 1 Pressed keys when the window is Lost focus
« Reply #4 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 :(

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10820
    • View Profile
    • development blog
    • Email
Re: [Help] counting 1 Pressed keys when the window is Lost focus
« Reply #5 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Made Y

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: [Help] counting 1 Pressed keys when the window is Lost focus
« Reply #6 on: May 03, 2017, 04:26:12 pm »
oh. okay, thankyou for the advice sir  ;)