Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: How do I display a string while typing it?  (Read 2023 times)

0 Members and 1 Guest are viewing this topic.

blueeyedlion

  • Jr. Member
  • **
  • Posts: 61
    • View Profile
How do I display a string while typing it?
« on: August 04, 2011, 09:08:14 pm »
I realize that this probably involves myString.GetText () and Event.Type.TextEvent, but I am a bit fuzzy on the details.  Could someone help me out?

Haikarainen

  • Guest
How do I display a string while typing it?
« Reply #1 on: August 04, 2011, 09:14:41 pm »
Just have a:
Code: [Select]
std::string buffer;

And on events:
Code: [Select]
if(Event.Type == sf::Event::TextEntered){
std::string tmp;
tmp+= static_cast<char>(Event.Text.Unicode);
buffer.append(tmp);
}


When drawing it, something like:
sf::Text a;
a.SetString(buffer);
sf::RenderWindow::Draw(a);


You might wanna tweak it to handle Enter/return, backspace etc differently.

Event.Key.Code's for :
Backspace = 8
Enter/return = 13


Edit: More complete example wich supports deleting strings and event on return:

Code: [Select]
if(Event.Type == sf::Event::TextEntered){
switch(Event.Key.Code){
case 8:
// Remove last character if pressed 8(backspace)
if(buffer.size()> 0){
buffer.erase(buffer.begin()+buffer.size()-1);
}
break;
case 13:
// When press enter, do stuff:
DoStuff();
break;
default:
// Not backspace or enter? Append character to bufferstring!
std::string tmp;
tmp+= static_cast<char>(Event.Text.Unicode);
buffer.append(tmp);
break;
}
}

blueeyedlion

  • Jr. Member
  • **
  • Posts: 61
    • View Profile
How do I display a string while typing it?
« Reply #2 on: August 04, 2011, 09:30:56 pm »
Thank you.