SFML community forums

Help => General => Topic started by: Lucy2020 on March 05, 2019, 07:37:07 am

Title: sf::Event::TextEntered is repeating Key Events;
Post by: Lucy2020 on March 05, 2019, 07:37:07 am
Hello all I would like to hopefully get some help with this problem.

Project : This is a personal project to write a simple text editor;

So whenever I press a Key(unicode assigned) it will repeat multiple times heres a list of what I tried and the code :

window.setKeyRepeatEnabled(false);

Used isKeyPressed for real time input vs KeyPressed but produced same output regardless;

// Unknown is caps lock but as of now it seems to not work due to unknown reasons
Tried to switch code to if(sf::Keyboard::keyPressed(Q) && sf::Keyboard::keyPressed(Unknown)){text.setString(input = input + "Q"); std::cout<<"random output" <<std::endl;} // produced no output with Unknown

I then was thinking to put a bool flag so you can't have repetition of characters 1 char away but then that would lead to issues when writing things like last names and etc.



Also this is running from the main loop so I just passed event as reference so thats not the problem;


 Here is the code , if anymore is needed I can provide
Quote
void Editor::Input(sf::Event& event) {


      //if (event.type == sf::Event::TextEntered) { if (event.text.unicode == 113) { text.setString(input = input + "q"); } }
      if (sf::Keyboard::isKeyPressed) { if (event.text.unicode == 113) { text.setString(input = input + "q"); } }
      if (event.type == sf::Event::TextEntered) { if (event.text.unicode == 119) { text.setString(input = input + "w"); } }
      if (event.type == sf::Event::TextEntered) { if (event.text.unicode == 101) { text.setString(input = input + "e"); } }
      if (event.type == sf::Event::TextEntered) { if (event.text.unicode == 114) { text.setString(input = input + "r"); } }
      if (event.type == sf::Event::TextEntered) { if (event.text.unicode == 116) { text.setString(input = input + "t"); } }
      if (event.type == sf::Event::TextEntered) { if (event.text.unicode == 121) { text.setString(input = input + "y"); } }
      if (event.type == sf::Event::TextEntered) { if (event.text.unicode == 117) { text.setString(input = input + "u"); } }
      if (event.type == sf::Event::TextEntered) { if (event.text.unicode == 105) { text.setString(input = input + "i"); } }
      if (event.type == sf::Event::TextEntered) { if (event.text.unicode == 111) { text.setString(input = input + "o"); } }
      if (event.type == sf::Event::TextEntered) { if (event.text.unicode == 112) { text.setString(input = input + "p"); } }


   
   }



Title: Re: sf::Event::TextEntered is repeating Key Events;
Post by: G. on March 05, 2019, 08:12:52 am
Are you calling your Input function from inside your while (window.pollEvent(event)) loop?
Title: Re: sf::Event::TextEntered is repeating Key Events;
Post by: Lucy2020 on March 05, 2019, 08:14:20 am
Yes int main()
{
   sf::RenderWindow window(sf::VideoMode(500, 500), "Text_Editor");
   
   Editor editor;
   
   while (window.isOpen())
   {
      sf::Event event;

      while (window.pollEvent(event))
      {
         if (event.type == sf::Event::Closed)
            window.close();
      }


      window.setKeyRepeatEnabled(false);


      window.clear(sf::Color::White);
      window.draw(editor.text);
      editor.Input(event);
      window.display();
   }
return 0;
}
Title: Re: sf::Event::TextEntered is repeating Key Events;
Post by: eXpl0it3r on March 05, 2019, 08:14:53 am
Also this is running from the main loop so I just passed event as reference so thats not the problem;
If you use the event object outside of the event loop, the event will be "frozen" to one state and it would seem like you keep repeating the same key press.

For posting code you should use the [code=cpp][/code] tags. :)
Title: Re: sf::Event::TextEntered is repeating Key Events;
Post by: G. on March 05, 2019, 08:16:08 am
No you're not.
Quote
      while (window.pollEvent(event))
      {
         if (event.type == sf::Event::Closed)
            window.close();
            // <-- HERE
      }


      window.setKeyRepeatEnabled(false);


      window.clear(sf::Color::White);
      window.draw(editor.text);
      editor.Input(event);// <-- this line should be at HERE
Title: Re: sf::Event::TextEntered is repeating Key Events;
Post by: Lucy2020 on March 05, 2019, 08:19:02 am
Whoops silly me ,seems your right and all the inputs work fine now , thanks for the help both of you and ill keep in mind to use the code tags next time  :D