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

Author Topic: sf::Event::TextEntered is repeating Key Events;  (Read 2859 times)

0 Members and 1 Guest are viewing this topic.

Lucy2020

  • Newbie
  • *
  • Posts: 4
    • View Profile
sf::Event::TextEntered is repeating Key Events;
« 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"); } }


   
   }



« Last Edit: March 05, 2019, 07:44:39 am by Lucy2020 »

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: sf::Event::TextEntered is repeating Key Events;
« Reply #1 on: March 05, 2019, 08:12:52 am »
Are you calling your Input function from inside your while (window.pollEvent(event)) loop?

Lucy2020

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: sf::Event::TextEntered is repeating Key Events;
« Reply #2 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;
}

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: sf::Event::TextEntered is repeating Key Events;
« Reply #3 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. :)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: sf::Event::TextEntered is repeating Key Events;
« Reply #4 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

Lucy2020

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: sf::Event::TextEntered is repeating Key Events;
« Reply #5 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
« Last Edit: March 05, 2019, 08:21:15 am by Lucy2020 »

 

anything