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;
	}

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.

Thanks for the input. It somehow fixed it, but after playing around some more with the inputString it seems very fishy :confused:

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.

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 == "

"[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

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*);*

_ if(tb*==8){print (“backspace”);}_
_ if(tb==13){print (“enter”);}
i++;}*_