Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by nesdroc · May 16, 2014 at 02:47 PM · inputkeypresskeydowninputstring

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;
     }
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image xortrox · May 16, 2014 at 02:50 PM 0
Share

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.

avatar image xortrox · May 16, 2014 at 02:51 PM 0
Share

As suspected it does add backspace escape sequences which should be handled.

avatar image nesdroc · May 17, 2014 at 12:15 PM 0
Share

I think there might be a problem with how the Input.inputString receives input as I've tried to suggest below.

4 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image xortrox · May 16, 2014 at 03:05 PM 1
Share

This is still not the proper way to handle backspaces with Input.inputString as seen here

avatar image CodeElemental · May 16, 2014 at 03:18 PM 1
Share

That example is simple usage scenario as a demonstration, not "proper way". Still, the desired behaviour can be achieved in different ways.

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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++;}
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges