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.

Checking if an input field is focused

Discussion in 'UGUI & TextMesh Pro' started by Panzerhandschuh, Oct 4, 2014.

  1. Panzerhandschuh

    Panzerhandschuh

    Joined:
    Dec 4, 2012
    Posts:
    17
    I know you can check if an input field is selected by the EventSystem with:
    Code (csharp):
    1. public static bool IsInputFieldFocused()
    2. {
    3.     GameObject obj = EventSystem.current.currentSelectedGameObject;
    4.     return (obj != null && obj.GetComponent<InputField>() != null);
    5. }
    This method isn't perfect though since the EventSystem will still consider the input field to be selected even after hitting enter/submitting it. This issue can become annoying for something like a chat box where you ignore game inputs while the text field is focused. When players submit their message by pressing enter, they still have to left/right/middle click somewhere else to force the EventSystem to set the "currentSelectedGameObject" to null.
     
    JasperCiti and nxrighthere like this.
  2. phil-Unity

    phil-Unity

    Unity UI Lead Developer Unity Technologies

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    just added a isFocused property to expose the internal state on if the input field will use events. Likely wont be in the next beta though due to it already being branched.
     
    Qwerku likes this.
  3. Panzerhandschuh

    Panzerhandschuh

    Joined:
    Dec 4, 2012
    Posts:
    17
    Thanks for adding that.