Hello there! I am completely new to SFML and C++ and right now I am working on a university project in which I have to program a game...
I need the name of the player, so I use sf::Event::TextEntered event to catch text input, and a sf::String to display it. Also, I follow some examples that I saw in this thread
https://en.sfml-dev.org/forums/index.php?topic=1451.0 that were really useful.
But the issue is that, although I can catch the input and show it, it catches more than one input for the same letter. For example, I press 'A', and it shows me 'AAA' o perhaps 'AAAA'.
Any help is welcome
while (window.isOpen())
{
//ReadInput
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
if (event.type == sf::Event::TextEntered)
{
player.insertName(static_cast<char>(event.text.unicode));
player.update();
}
...
void Player::update()
{
_playerText.setString(_playerInput);
}
void Jugadorxs::innsertName(int unicode)
{
if (unicode == 8) { //If backspace
if (textSize > 0)
_playerInput.erase(static_cast<size_t>(textSize) - 1, 1);
}
else if (unicode >= 32 && unicode <= 126) {
_playerInput += (char)unicode;
}
else if (unicode >= 192 && unicode <= 255) {
_playerInput += (char)unicode;
}
}