Ah, I didn't realize s1 was a
sf::String.
I took your code and made a minimal example out of it and it seems to work fine.
#include <SFML/Graphics.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "Test");
window.setVerticalSyncEnabled(true);
sf::Font font;
font.loadFromFile("arial.ttf");
sf::String s1;
sf::Text txt(s1, font);
while(window.isOpen())
{
sf::Event event;
while(window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
window.close();
}
if (event.type == sf::Event::TextEntered)
{
if (event.text.unicode == '\b')
{
if (s1.getSize() > 0)
s1.erase(s1.getSize() - 1, 1);
}
else if (event.text.unicode < 128)
{
s1 += static_cast<char>(event.text.unicode);
}
txt.setString(s1);
}
}
window.clear();
window.draw(txt);
window.display();
}
}
I guess your error must come from a different place than what you thought.
I noticed that with the posted code, you're still adding the backspace character to the string, as such I moved the size condition inside the other if block and added an else if for the second block.