SFML community forums

Help => Window => Topic started by: Qluxzz on May 11, 2013, 02:10:26 pm

Title: Text entered but update when keyreleased
Post by: Qluxzz 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 :)
Title: Re: Text entered but update when keyreleased
Post by: AlexAUT 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
Title: Re: Text entered but update when keyreleased
Post by: Laurent 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).
Title: Re: Text entered but update when keyreleased
Post by: Laurent 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.
Title: Re: Text entered but update when keyreleased
Post by: Laurent 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 ;)
Title: Re: Text entered but update when keyreleased
Post by: Laurent 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.