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

Author Topic: TextEntered input repeats  (Read 1361 times)

0 Members and 1 Guest are viewing this topic.

gonzalo_

  • Newbie
  • *
  • Posts: 2
    • View Profile
TextEntered input repeats
« on: July 06, 2022, 01:39:07 am »
Hello there! I am completely new to SFML and C++ and right now I am working on a university project in which I have to program a game...

I need the name of the player, so I use  sf::Event::TextEntered event to catch text input, and a sf::String to display it. Also, I follow some examples that I saw in this thread https://en.sfml-dev.org/forums/index.php?topic=1451.0 that were really useful.

But the issue is that, although I can catch the input and show it, it catches more than one input for the same letter. For example, I press 'A', and it shows me 'AAA' o perhaps 'AAAA'. 

Any help is welcome

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

    if (event.type == sf::Event::TextEntered)
    {
        player.insertName(static_cast<char>(event.text.unicode));
        player.update();
    }
...


Quote
void Player::update()
{
        _playerText.setString(_playerInput);
}

Quote
       
void Jugadorxs::innsertName(int unicode)
{
if (unicode == 8) { //If backspace
                if (textSize > 0)
                        _playerInput.erase(static_cast<size_t>(textSize) - 1, 1);
        }
        else if (unicode >= 32 && unicode <= 126) {
                _playerInput += (char)unicode;
        }
        else if (unicode >= 192 && unicode <= 255) {
                _playerInput += (char)unicode;
        }    
}

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: TextEntered input repeats
« Reply #1 on: July 06, 2022, 07:34:08 am »
Never use event outside of your pollEvent loop

gonzalo_

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: TextEntered input repeats
« Reply #2 on: July 07, 2022, 02:37:30 am »
Great, that was the trouble. Thank you very much!!!

 

anything