Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Register Return on TextField

Discussion in 'Immediate Mode GUI (IMGUI)' started by Der Dude, Nov 5, 2007.

  1. Der Dude

    Der Dude

    Joined:
    Aug 7, 2006
    Posts:
    213
    Hello,

    is there a smooth way of registering when a user hits return while typing in a TextField? The string the TextField method returns never contains the character '\n'.

    I know I can check Input.inputString to see if return was pressed but how do I check if the TextField is in Focus?
     
  2. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    In my prototyping test I also had issues getting focus off the TextField, so when the user pressed the arrow keys to move, it filled the TextField with a's and d's.

    I couldn't find any docs on setting focus, besides setting how the elements recieve KB focus, but that didn't seem to be what was needed. I admit I didn't look too long though.

    I would be very interested to know this too.

    Thanks,
    -Jeremy
     
  3. shaun

    shaun

    Joined:
    Mar 23, 2007
    Posts:
    728
    maybe something with Event.type and input char - I haven't tried this, but if it can work for tabbing ("\t") in a textfield, it should also work for CR.
     
  4. hsparra

    hsparra

    Joined:
    Jul 12, 2005
    Posts:
    750
    Here is something from before the new GUI system that read the enter key and would change focus:
    Code (csharp):
    1.  
    2. if (c == "\n"[0] || c == "\t"[0]) {
    3.   if (inputText.Length)
    4.   {
    5.     guiText.text=inputText;
    6.     NotifyEdit();
    7.     return;
    8.   }
    9. }
    10.  
    "c" is just a character from Input.inputString and inputText is just a temporary variable used for some stuff. NotifyEdit() just sent a message to the next object that was suppose to receive focus, which started a coroutine to receive input or performed some other action.
     
  5. Der Dude

    Der Dude

    Joined:
    Aug 7, 2006
    Posts:
    213
    In the old GUI its no problem really. I was just hoping I would get around writing my own GUI. Having no Objects to deal with is a plus when it comes to getting something done fast, but it just lacks many control features needed to make a good GUI.
    Too bad.
     
  6. Der Dude

    Der Dude

    Joined:
    Aug 7, 2006
    Posts:
    213
    Thought I'd share my workaround with you guys.

    If you want to register when someone presses return on the keyboard while in a TextField, then you should really use a TextArea. Look in the string returned for the character '\n'.

    Something like this:

    Code (csharp):
    1.  
    2. textFieldStr = GUI.TextArea( new Rect( 50, 100, 500, 20 ), textFieldStr );
    3.        
    4. int indexOf = textFieldStr.IndexOf( '\n' );
    5.        
    6. while( indexOf != -1 )
    7. {
    8.     string inputTillEnter = textFieldStr.Substring( 0, indexOf );
    9.            
    10.     textFieldStr = textFieldStr.Substring( indexOf+1 );
    11.     indexOf = textFieldStr.IndexOf( '\n' );
    12. }
    13.  
     
  7. shaun

    shaun

    Joined:
    Mar 23, 2007
    Posts:
    728
    To grab return you can use this method, it doesn't matter which control you are focused on, it will activate the button. (good for modal dialogues)

    Code (csharp):
    1.         if (GUILayout.Button("Login") || (Event.current.character == char.Parse("\n")  Event.current.type == EventType.keyUp))
    2.         {
    3.          //Do this if i click of hit enter
    4.         }

    I was hoping to be able to check which window has Focus, which would make this useful when multiple forms are active, but there doesn't seem to be a property for that. Anyone else have any ideas?

    Thx
    Shaun