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

Author Topic: Text Input - Handling press of backspace.  (Read 13115 times)

0 Members and 1 Guest are viewing this topic.

GameAddict

  • Newbie
  • *
  • Posts: 1
    • View Profile
Text Input - Handling press of backspace.
« on: June 15, 2012, 01:19:14 am »
I've been messing around with this for a while, and I'm having a bit of trouble getting this to work. I'm trying to write a simple text input system using sf::Text and sf::String, and nothing I've tried has gotten it to handle pressing backspace.

Here is my code:
#include <SFML/Graphics.hpp>
#include <string>
#include <iostream>
using namespace std;

int main()
{
    sf::RenderWindow App(sf::VideoMode(800, 600), "SFML Text Input System");
    sf::String Text;
    string str;

    const sf::Input & Input = App.GetInput();

    while(App.IsOpened())
    {
        sf::Event Event;
        while(App.GetEvent(Event))
        {
            if (Event.Type == sf::Event::Closed) App.Close();
            if (Event.Type == sf::Event::TextEntered)
            {
                if (Event.Text.Unicode < 128)
                    str += static_cast<char>(Event.Text.Unicode);
            }
            Text.SetText(str);
        }

        App.Clear();
        App.Draw(Text);
        App.Display();
    }
}
 

Any help is greatly appreciated.  :)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Text Input - Handling press of backspace.
« Reply #1 on: June 15, 2012, 08:10:23 am »
You must handle it explicitely, and instead of adding a character, in this case you must remove the last one.
Laurent Gomila - SFML developer

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10815
    • View Profile
    • development blog
    • Email
Re: Text Input - Handling press of backspace.
« Reply #2 on: June 16, 2012, 10:51:30 am »
I'm not quite sure what Laurent suggests by saying handle it explicitely' but you could always use sf::Keyboard or the KeyPressed/KeyReleased events.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Text Input - Handling press of backspace.
« Reply #3 on: June 16, 2012, 11:50:02 am »
I mean this:
if (Event.Text.Unicode == '\b')
    str.erase(str.size() - 1, 1);
else if (Event.Text.Unicode < 128)
    str += static_cast<char>(Event.Text.Unicode);
Laurent Gomila - SFML developer

dove96

  • Newbie
  • *
  • Posts: 20
    • View Profile
    • Email
Re: Text Input - Handling press of backspace.
« Reply #4 on: March 24, 2016, 02:48:07 am »
Hi, everyone i hope this thread is not dead yet, i just want to ask why is my code(below) only delete the last character when it reaches the total size of character input.

if (event.type == sf::Event::TextEntered)
                            {   //handle user input     !!works properly
                                if((event.text.unicode < 128)&& playerInput.getSize()< 8 )
                                    {   
                                       playerInput.insert(playerInput.getSize(), event.text.unicode);
                                       playerText.setString(playerInput);
                                    }
                                //erase on backspace press     !!does not work properly?what is wrong with it??
                                if(event.text.unicode == '\b')
                                    {
                                        playerInput.erase(playerInput.getSize() -1,1);
                                    }
                            }
what i want to do is ask for input(max of 8 characters), and delete when backspace is entered.
« Last Edit: March 24, 2016, 02:54:57 am by dove96 »
-dove96-

G.

  • Hero Member
  • *****
  • Posts: 1592
    • View Profile
Re: Text Input - Handling press of backspace.
« Reply #5 on: March 24, 2016, 02:59:30 am »
Don't worry it's only been dead for 3+ years...

The solution is in the first reply of this thread:
You must handle it explicitely, and instead of adding a character, in this case you must remove the last one.
In your code you add '\b' to your string (when it's not already 8 characters long) and then you delete the last character, which is '\b'.

dove96

  • Newbie
  • *
  • Posts: 20
    • View Profile
    • Email
Re: Text Input - Handling press of backspace.
« Reply #6 on: March 24, 2016, 03:23:42 am »
i dont really get what "handle explicitly", how do i do it? im just knew in SFML, thats why, i read the documentation on .erase(position, count o char to erase), could you please give me a clearer hint that i could understand.
:)

 if (event.type == sf::Event::TextEntered)
                            { 
                                if((event.text.unicode < 128)&& playerInput.getSize()< 8 )
                                    { 
                                       playerInput.insert(playerInput.getSize(), event.text.unicode);
                                       playerText.setString(playerInput);
                                    }
                            }
                     if(event.type == sf::Event::KeyPressed)
                            {
                                if(event.key.code == sf::Keyboard::BackSpace)
                                    playerInput.erase(playerInput.getSize()-1,1);
                            }

this one works it does delete the last character but the backspace value remains.

if "HELLU" -press backspace "HELL" -press O "HELL 0".

deletes only once.
« Last Edit: March 24, 2016, 03:49:40 am by dove96 »
-dove96-

bitano

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
Re: Text Input - Handling press of backspace.
« Reply #7 on: March 24, 2016, 08:07:55 am »
Laurent already posted a working piece of code, but here it is again, using your vars :-)
Assuming playerInput is an sf::String.

if (event.type == sf::Event::TextEntered) {  
    if (Event.Text.Unicode == '\b') { // handle backspace explicitly
        playerInput.erase(playerInput.size() - 1, 1);
    } else { // all other keypresses
        playerInput += static_cast<char>(Event.Text.Unicode);
    }
}
 

dove96

  • Newbie
  • *
  • Posts: 20
    • View Profile
    • Email
Re: Text Input - Handling press of backspace.
« Reply #8 on: March 24, 2016, 08:31:00 am »
Thank you bitano.. got it working.. Thank you for helping beginners like me, i really learn very slow. Thank you for your help.

if (event.text.unicode == '\b') // handle backspace explicitly
                                {
                                    playerInput.erase(playerInput.getSize()- 1, 1);
                                    playerText.setString(playerInput);
                                }
                            else // all other keypresses
                                {
                                    playerInput += static_cast<char>(event.text.unicode);
                                    if((event.text.unicode < 128)&& (playerInput.getSize()< 8 ) )
                                            playerText.setString(playerInput);
                                }
-dove96-

G.

  • Hero Member
  • *****
  • Posts: 1592
    • View Profile
Re: Text Input - Handling press of backspace.
« Reply #9 on: March 24, 2016, 05:24:32 pm »
Posting code already posted = helpful
Trying to explain = useless

 ::) :-\ :'(

dove96

  • Newbie
  • *
  • Posts: 20
    • View Profile
    • Email
Re: Text Input - Handling press of backspace.
« Reply #10 on: March 24, 2016, 07:07:31 pm »
Posting code already posted = helpful
Trying to explain = useless
You see i'm really new to programming, sorry that i forgot to thank you, you explained it well but i didnt know how to implement it...now im getting it.  ;D ;D ;D

-dove96-

 

anything