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

Author Topic: Text entered but update when keyreleased  (Read 3078 times)

0 Members and 2 Guests are viewing this topic.

Qluxzz

  • Guest
Text entered but update when keyreleased
« on: May 11, 2013, 02:10:26 pm »
Hi there I'm working on a textbox where the user enters its name. The problem I'm having is that a soon as i press a key it writes out the character about five times in a row. That's why I just want it to enter a character when the user releases the key.

if(mainEvent.type == sf::Event::TextEntered)
        {      
                char c = static_cast<char>(mainEvent.text.unicode);
                if(c >= 'A' && c <= 'z')
                {
                        // Add the new character to the string
                        str = c;
                        // Set the string to name
                        nameString += str;

                                name.setString(nameString);
                                cout << nameString;
                       
                }
        }

Thanks in advance :)

AlexAUT

  • Sr. Member
  • ****
  • Posts: 396
    • View Profile
Re: Text entered but update when keyreleased
« Reply #1 on: May 11, 2013, 02:22:00 pm »
If you really only want 1 character each KeyPressed you could use "window.setKeyRepeatEnabled(false);"

But normally sf::Event::TextEntered return the character only once and when you hold the key longer than (i guess 1second) it will start "spamming" the character, like every Windows Inputbox does.


AlexAUT

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Text entered but update when keyreleased
« Reply #2 on: May 11, 2013, 03:27:21 pm »
Can you show the surrounding code? The complete event loop would be great (I don't care about other events, I just want to see how/where your code block is executed).
Laurent Gomila - SFML developer

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Text entered but update when keyreleased
« Reply #3 on: May 11, 2013, 04:44:41 pm »
Can you show the calling code? I'd like to see where the event instance comes from.
Laurent Gomila - SFML developer

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Text entered but update when keyreleased
« Reply #4 on: May 11, 2013, 07:13:27 pm »
Ok, let's be more explicit. The event instance is initialized by a call to window.pollEvent. I want to see this call, and what happens to the event until it reaches userInput ;)
Laurent Gomila - SFML developer

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Text entered but update when keyreleased
« Reply #5 on: May 11, 2013, 08:46:31 pm »
Any usage of mainEvent should happen inside the event loop. Outside, its contents are undefined, or if it has a wider scope, it will always be the last event that happened repeating again and again -- which explains your problem.
Laurent Gomila - SFML developer