SFML community forums

Help => Graphics => Topic started by: Heryon on June 21, 2017, 02:58:26 am

Title: sf::Text displaying square
Post by: Heryon on June 21, 2017, 02:58:26 am
Hi, I have this code:
Code: [Select]
void TextEditor::handleEvent(const sf::Event& p_event)
{
   if(p_event.type == sf::Event::KeyPressed && p_event.key.code == sf::Keyboard::Return)
      addLine();

   else if(p_event.type == sf::Event::TextEntered)
      write(p_event);
}

void TextEditor::addLine()
{
   sf::String s(m_text.getString())
   s += "\n";
   m_text.setString(s);
}

void TextEditor::write(const sf::Event& p_event)
{
   sf::String s(m_text.getString());

   if(p_event.key.code != sf::Keyboard::BackSpace && p_event.key.code != sf::Keyboard::Return)
      s += static_cast<char>(p_event.text.unicode);

   m_text.setString(s);
}

When I press Return with this code, the Text goes to the next line but also writes a square at the beginning of the line.
If I comment the conditions which calls write(), no square is printed.
So why am I getting this strange behaviour?
Title: Re: sf::Text displaying square
Post by: Mario on June 22, 2017, 12:15:44 pm
The line break is a character you'll receive as sf::Event::TextEntered, but it's not printable (therefore resulting in the square box).