- Home /
Key reading from inputString, backspace only works the first time
Hi, I'm trying to read what the user is typing and displaying on screen. Letters typed are currently getting displayed fine.
Problem: Hitting backspace in order to remove letters only works the first time.
Any idea what's wrong?
public GUIText guiText;
string _storeInput;
// Use this for initialization
void Start () {
_storeInput = null;
}
void Update () {
//if backspace remove last char
if (Input.GetKeyDown(KeyCode.Backspace) && _storeInput.Length > 0) {
_storeInput = _storeInput.Remove(_storeInput.Length - 1);
}
else {
//add input
_storeInput += Input.inputString;
}
Debug.Log(_storeInput.Length);
//display
guiText.text = _storeInput;
}
Did you test this without the use of backspace at all? I would guess that Input.inputString might already handle backspaces using the escape sequence for backspace.
As suspected it does add backspace escape sequences which should be handled.
I think there might be a problem with how the Input.inputString receives input as I've tried to suggest below.
Answer by CodeElemental · May 16, 2014 at 02:56 PM
I think the issue here is the Input.GetKeyDown(KeyCode.Backspace) . It detects when the user initially presses the button, so when you hold it down it is ignored. Try using GetKey instead.
This is still not the proper way to handle backspaces with Input.inputString as seen here
That example is simple usage scenario as a demonstration, not "proper way". Still, the desired behaviour can be achieved in different ways.
Answer by nesdroc · May 17, 2014 at 12:39 AM
Thanks for the input. It somehow fixed it, but after playing around some more with the inputString it seems very fishy :/
Having only the following in my update function yields unexpected results. I'm not sure if it's consistent, but from my debug log the inputString contains input one update after the Input.anyKey has fired. Which would explain why it doesn't work with anyKeyDown because anyKeyDown would fire, but inputString would be empty for that update step. Thus, the following update step when inputString contains the key pressed anyKeyDown is no longer true.
if (Input.anyKey)
Debug.Log(Input.inputString);
I would be grateful if someone could elaborate on whether this is a bug or if I have missed something.
Answer by Minchuilla · Jun 22, 2014 at 05:13 PM
For later viewers...
I had this problem myself and solved it by adapting the code found in the docs, which lots of other people seem to have referenced to. For myself, the input string would not seem to work for backspaces so what I did was this:
foreach(char c in Input.inputString)
{
if(c != "\b"[0]) //check that the key is not a backspace
{
if ((c == "\n"[0] || c == "\r"[0])) //if return was pressed
print("The text entered: " + guiText.text);
else
guiText.text += c; //if anything else was pressed
}
}
if(Input.GetKeyDown(KeyCode.Backspace) && guiText.text.Length != 0)
guiText.text = guiText.text.Substring(0, guiText.text.Length - 1);
Here if the input string detects a backspace it does nothing and then later GetKey/Down acts on the backspace being pressed.
It's important for the input string to enter nothing if a backspace was pressed because it otherwise add a character to the guiText which would the be removed a few lines later causing, for the user, nothing to happen which is the problem.
Hope this clarified a few things.
Minchuilla
Answer by toddisarockstar · Apr 19, 2020 at 02:59 AM
input.string seems to loose keys like backspace and enter whenever displayed as a string. However you can still see these keys if you cast to a byte array. if you look at an ascii chart, backspace is 8. and enter is 13.
byte[] tb = System.Text.Encoding.ASCII.GetBytes(Input.inputString);
int i = 0;
while(i<tb.Length){
print (tb[i]);
if(tb[i]==8){print ("backspace");}
if(tb[i]==13){print ("enter");}
i++;}
Your answer
Follow this Question
Related Questions
Odd input glitch 2 Answers
Perform event only once on keypress 1 Answer
Control config assignments 2 Answers
C# Script Simulating Input.inputString 0 Answers
Animation Keypress Trigger Problem 0 Answers