adambarkertreso adambarkertreso - 1 month ago 13
C++ Question

When using textEntered I can't get the backspace key to delete last character of the string

I am trying to create a text box a user an input data into, it is going fine, however, whenever I try to set up the backspace key to delete the last character of the string, it doesn't seem to work even though countless tutorials have showed me this way.

if (event.type == sf::Event::TextEntered)
{
sentence += (char)event.text.unicode;
//if (sentence.getSize() < 16) {

//

if (event.text.unicode == 8)
{

sentence.erase(sentence.getSize() - 1, sentence.getSize());

}
text.setString(sentence);

break;
//}
}


Also, was wondering what would be the best way to stop the string from advancing 16 characters.

Answer

Nevermind, its not that hard, you're going to feel weird after what i'm going to say ^^

if (event.type == sf::Event::TextEntered) {
    if (event.text.unicode == 8)
        if (sentence.getSize())//If the string doesn't have any char, don't do anything
            sentence.erase(sentence.getSize() - 1, sentence.getSize());
    else //Don't add any character when you delete one
        if(sentence.getSize() < 16)//Yeah, thats the best way to do it
            sentence += (char)event.text.unicode;

    text.setString(sentence);
    break;
}

If you were pressing backspace, your program was adding it to the string and then deleting it... so... it wasn't doing anything ^^

Comments