Search Unity

How to commit editting in text field by pressing enter ?

Discussion in 'Immediate Mode GUI (IMGUI)' started by amirghayes, Aug 13, 2011.

  1. amirghayes

    amirghayes

    Joined:
    Apr 8, 2011
    Posts:
    98
    Hi
    Like the title says , how can I do that
    I have a text field inside a window , I want some function to run if I press enter while typing , like erasing the typed string in the text field and set a label to have this string
    whatever I tried to do while the text field have focus , nothing happens when I press enter

    I tried couple of things but nothing happens

    I try this

    Code (csharp):
    1.  
    2.         chatting = GUI.TextField(new Rect(5, boxHeight - 60, 300, 50), chatting, skin.customStyles[13]);
    3.             if (Event.current.Equals(Event.KeyboardEvent("Return")))
    4.             {
    5.                 Debug.Log("Pressed Enter");
    6.                 DoneTyping();
    7.             }
    8.  
    9.  
    and nothing happens
    I tried to replace Return with return but same result
    if I take this if and put it in the update function I get NullReferenceException error

    another try

    I've put the next code in the update function

    Code (csharp):
    1.  
    2.         if (Input.GetKey(KeyCode.Return))
    3.         {
    4.             DoneTyping();
    5.         }
    6.  
    7.  
    this do what I want but only if the text field doesn't have focus

    I did some other tests too playing around with the focus but with no avail

    if anyone have an idea on how to run a function or event by pressing enter while I'm typing in text field in a window that would be a great help

    Thanks in advance
     
    Last edited: Aug 14, 2011
  2. zine92

    zine92

    Joined:
    Nov 13, 2010
    Posts:
    1,347
    Try this.
    Code (csharp):
    1.  
    2. var chatting : String;
    3. function OnGUI()
    4. {
    5.     chatting = GUI.TextField(new Rect(5, 100, 300, 50), chatting);
    6.     if (Event.current.Equals(Event.KeyboardEvent("None")))
    7.     {
    8.         Debug.Log("Pressed Enter");
    9.         DoneTyping();
    10.     }
    11. }
    12.  
    13. function DoneTyping()
    14. {
    15.     chatting = "YESSSS";
    16. }
    17.  
    It should work and after you finished typing and press enter the text field will show YES. :D
     
  3. amirghayes

    amirghayes

    Joined:
    Apr 8, 2011
    Posts:
    98
    Thank you very much zine92
    this do exactly what I wanted to do

    would u please explain it a bit ? I didn't got it how with event = none and it works when I press enter ? is it kinda wiered ?
    I ask cause I wouldn't ever come to this solution if I didn't post the question and an awsome member like u replied

    Thank you again and keep up the good work
     
    Last edited: Aug 14, 2011