-
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. :)
-
You must handle it explicitely, and instead of adding a character, in this case you must remove the last one.
-
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.
-
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);
-
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.
-
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'.
-
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.
-
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);
}
}
-
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);
}
-
Posting code already posted = helpful
Trying to explain = useless
::) :-\ :'(
-
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