简体   繁体   中英

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.

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 ^^

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM