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

Author Topic: Deleting text  (Read 4270 times)

0 Members and 1 Guest are viewing this topic.

Azaral

  • Full Member
  • ***
  • Posts: 110
    • View Profile
Deleting text
« on: December 11, 2012, 06:41:10 pm »
So, I'm trying to make a simple text field for user to type information, names etc. I can get the text input just find and display it just fine, however, what I haven't managed to do so far is to get the ability to backspace to work. Some help would be appreciated.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Deleting text
« Reply #1 on: December 11, 2012, 06:53:05 pm »
What's your problem? Catching the backspace key is easy, deleting the last character of a string is easy too, so... ? ;)
Laurent Gomila - SFML developer

Azaral

  • Full Member
  • ***
  • Posts: 110
    • View Profile
Re: Deleting text
« Reply #2 on: December 11, 2012, 09:08:53 pm »
Well, this is what I'm doing. It's been a while since I've fiddled with changing the stuff inside of strings.

Code: [Select]

if(systemEvents.Key.Code == sf::Key::Back)
{
text.erase(text.end());
}


I'm probably doing both things wrong for all I know.
This is after I've checked if it is a text entered event.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Deleting text
« Reply #3 on: December 11, 2012, 10:27:38 pm »
text.end() points to one element past the last one. It doesn't refer to a valid character. Try erase(text.rbegin()), or erase(text.size() - 1) instead.
Laurent Gomila - SFML developer

Azaral

  • Full Member
  • ***
  • Posts: 110
    • View Profile
Re: Deleting text
« Reply #4 on: December 11, 2012, 10:35:49 pm »
Well, I tried text.end() -1 and no change. I thought maybe the display method was not correct so I had the text change position as the string got longer. I held the back key down and it indicated the string was just getting bigger instead of smaller

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Deleting text
« Reply #5 on: December 11, 2012, 10:37:50 pm »
Checking keycode after identifying event as text entered = bad?
Text entered would have 8 as text which is backspace. KeyPressed would have back as keycode.
Back to C++ gamedev with SFML in May 2023

Azaral

  • Full Member
  • ***
  • Posts: 110
    • View Profile
Re: Deleting text
« Reply #6 on: December 12, 2012, 03:58:38 am »
OK I got it figured out.

Code: [Select]
if(systemEvents.Text.Unicode == 8 && text.end() != text.begin())
{
text.erase(text.end() -1);
}

else
{
text += systemEvents.Text.Unicode;
}
« Last Edit: December 12, 2012, 04:00:19 am by Azaral »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Deleting text
« Reply #7 on: December 12, 2012, 07:34:51 am »
'\b' would be better than 8 ;)
Laurent Gomila - SFML developer

Azaral

  • Full Member
  • ***
  • Posts: 110
    • View Profile
Re: Deleting text
« Reply #8 on: December 12, 2012, 01:28:19 pm »
Why's that?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Deleting text
« Reply #9 on: December 12, 2012, 02:15:48 pm »
\b is the escape sequence that represents the backspace ASCII character.
Laurent Gomila - SFML developer

 

anything