Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Focus on InputField Programmatically

Discussion in 'Unity UI (uGUI) & TextMesh Pro' started by porglezomp, Aug 25, 2014.

  1. porglezomp

    porglezomp

    Joined:
    Aug 21, 2012
    Posts:
    40
    I have an input field that's popping up and covering the screen, and it doesn't make sense to force the user to click on it, since they have to interact with it before I let them do anything else.

    Is there a way to give an InputField the focus?
     
    ThiefZero likes this.
  2. Jmorr10

    Jmorr10

    Joined:
    Jul 6, 2012
    Posts:
    2
    Ok. So after a bit of messing around with this, I came up with a solution that worked for me. I read (somewhere) on here that there are actually two parts to InputField activation... The first one is being selected, the second one is being clicked. Or something to that effect, anyway..

    The code that I used was:

    EventSystemManager.currentSystem.SetSelectedGameObject(inputField.gameObject, null);
    inputField.OnPointerClick (null);

    This code assumes that inputField is an instance of an InputField component.

    Hope that works for you.
     
  3. FredZvt81

    FredZvt81

    Joined:
    Apr 8, 2014
    Posts:
    20
    Thanks, Jmorr10!
    Your solution worked for me.
     
    tachyon-particle likes this.
  4. giano574

    giano574

    Joined:
    Nov 28, 2013
    Posts:
    76
    Hi. I am trying to do the same but my for some reason I get a NullReferenceException on the line:

    Code (CSharp):
    1. input.OnPointerClick(null);
    Here is the full code.

    Code (CSharp):
    1. void Update()
    2. {
    3.     if (Input.GetKeyDown(KeyCode.A))
    4.     {
    5.         EventSystem.current.SetSelectedGameObject(input.gameObject, null);
    6.         input.OnPointerClick(null);
    7.     }
    8. }
    The first line works fine. I hope you can help me.
     
    Last edited: Oct 3, 2014
    trappist-1 and hamokshaelzaki like this.
  5. Marble

    Marble

    Joined:
    Aug 29, 2005
    Posts:
    1,210
    Are you using Beta 20? This method appears to have been broken / changed in the upgrade.
     
  6. Kesh

    Kesh

    Joined:
    Jan 13, 2014
    Posts:
    15
    Yeah, this doesn't work anymore in B20. I would love to find a new solution.
     
  7. Kesh

    Kesh

    Joined:
    Jan 13, 2014
    Posts:
    15
    This seems to work now:

    Code (CSharp):
    1. EventSystem.current.SetSelectedGameObject(inputField.gameObject, null);
    2. inputField.OnPointerClick(new PointerEventData(EventSystem.current));
     
    ThiefZero, hamokshaelzaki and Vitaliq like this.
  8. aprotasenya

    aprotasenya

    Joined:
    Jun 21, 2014
    Posts:
    1
    Thanks, Kesh! At least it does not give the NullReferenceException errors anymore.

    Still, if I put this into the Start () function, it does not allow me to type in the InputField once the scene has loaded. Could I be missing anything? Should I be using some special interface for this to work (like IPointerClickHandler, for instance)?
     
    sihame likes this.
  9. phil-Unity

    phil-Unity

    Unity UI Lead Developer Unity Technologies

    Joined:
    Nov 23, 2012
    Posts:
    1,140
    I'd call inputfield.ActivateInputField() not OnPointerClick because OnPointerClick checks to see if the eventData.button is "PointerEventData.InputButton.Left".


    Code (CSharp):
    1. public virtual void OnPointerClick(PointerEventData eventData)
    2.     {
    3.         if (eventData.button != PointerEventData.InputButton.Left)
    4.             return;
    5.  
    6.         ActivateInputField ();
    7.     }
     
  10. Kesh

    Kesh

    Joined:
    Jan 13, 2014
    Posts:
    15
    I have my code in a function that toggles the InputField on and off for a chat input. I'd just put the code where ever you are calling your InputField functionally. Should work in Start() unless you are interacting with different UI elements on loading.

    Maybe use an Invoke to see if you can get it to work after a few seconds.
     
  11. Kesh

    Kesh

    Joined:
    Jan 13, 2014
    Posts:
    15
    It looks like using ActivateInputField(); or OnPointerClick now adds a line break into the inputField before focusing in RC1.

    This is the case with single or multi-line inputFields.
     
  12. phil-Unity

    phil-Unity

    Unity UI Lead Developer Unity Technologies

    Joined:
    Nov 23, 2012
    Posts:
    1,140
    Hmm that doesnt seem right, could you report a bug please.
     
  13. Kesh

    Kesh

    Joined:
    Jan 13, 2014
    Posts:
    15
    Not a bug. My mistake. I was using KeyCode.Return to focus the inputField but was calling it with GetKeyDown instead of GetKeyUp which was adding the linebreak after the focus. All is good.
     
    arommelaere and phil-Unity like this.
  14. jlcra

    jlcra

    Joined:
    Jan 27, 2014
    Posts:
    13
    Thanks for this!

    However, while it seems to work if I call it from Start(), it doesn't work if called from OnEnable(). If I put the code in OnEnable(), the field does not get focus (though there are no errors). I wonder if there's some possibility like the parent is enabled before the children (the input field) so activating it does nothing. Anyone have any ideas?
     
  15. phil-Unity

    phil-Unity

    Unity UI Lead Developer Unity Technologies

    Joined:
    Nov 23, 2012
    Posts:
    1,140
    I believe i just recently fixed this bug and it hasn't been released yet. The issue is the eventSystem was doing something weird in terms of setting the selected to NULL the first update loop which would confuse the input field. if you want to download the open source UI repo i can tell you where you can put the fix.
     
  16. jlcra

    jlcra

    Joined:
    Jan 27, 2014
    Posts:
    13
    Thanks for the quick response!

    This fix isn't super urgent for us, so I can just wait for the next release.

    I did do a check though, in the parent OnEnable(), the IsActive property of the InputField is false...not sure if that matters, but it sounds like you've already fixed it =)
     
  17. walther

    walther

    Joined:
    Aug 30, 2014
    Posts:
    1
    ActivateInputField() still adds an invisible newline for me in Unity 4.6.1f1 (free version)... Only fix I've managed to come up with so far was to set ContentType of the InputField to Alphanumeric. Then it suddenly works as expected.
     
  18. rorakin3

    rorakin3

    Joined:
    Jan 2, 2013
    Posts:
    453
    Thanks for the suggestion, but doesn't work with space characters. Changing "Line Type" to "Multi Line Submit" and Content Type = Standard seems to work for me.
     
  19. michidk

    michidk

    Joined:
    Feb 18, 2014
    Posts:
    4
    Yeah that works... just use:
    Code (CSharp):
    1. inputField.ActivateInputField();
    2. inputField.Select();
    and change the the Content Type to "Multi Line Submit" or "Multi Line Newline"
     
    Last edited: Jan 28, 2015
    seansteezy likes this.
  20. roger-wang

    roger-wang

    Joined:
    Jun 8, 2013
    Posts:
    11
    void Update()
    {
    if (Input.GetKeyDown(KeyCode.A))
    {
    InputField input = this.GetComponent<InputField>();

    //方案1
    //EventSystem.current.SetSelectedGameObject(input.gameObject, null);
    //input.OnPointerClick(new PointerEventData(EventSystem.current));

    //方案2
    input.ActivateInputField();
    input.Select();
    }
    }

    two way can do this. i'm Unity 5.1.1
     
  21. Julian-G

    Julian-G

    Joined:
    Sep 13, 2012
    Posts:
    2
    On Unity 5.2

    Apparently all you need is to call ActivateInputField()

    InputField inputField = someGameObject.GetComponent<InputField>();
    inputField.ActivateInputField();
     
    crash664 likes this.
  22. Athalos

    Athalos

    Joined:
    Jul 21, 2014
    Posts:
    23
    Any way to get this to show the carret? I am using an on-screen keyboard for a native build. This steals focus from the inputField and I want to give it back after I update the inputField.text component. I can get this to work aside from the fact that the carret is missing and the inputField acts as though it has been clicked once for editing instead of twice for appending. All the text highlights. Any thoughts?
     
    fafase likes this.
  23. Hydropulse17

    Hydropulse17

    Joined:
    Jan 26, 2014
    Posts:
    22
    I realize this is a bit of a necro, but I just want to confirm that this code still works fine, since it's simple and clean... like the way you're making me feel tonight, it's hard to let it go.

    Code (CSharp):
    1. inputField.ActivateInputField();
    2. inputField.Select();
    If you want it to return focus every time you submit, put it in the function called by On End Edit via the inputField component. You might also want to include
    Code (CSharp):
    1. inputField.text = "";
    to start fresh each time it auto-focuses.
     
    slake_it, CoughE and gresolio like this.
  24. PinkTaco97

    PinkTaco97

    Joined:
    May 25, 2017
    Posts:
    1
    To set the InputField to selected:

    Code (CSharp):
    1. EventSystem.current.SetSelectedGameObject(GameObject);
    To set the InputField to Unselected

    Code (CSharp):
    1. EventSystem.current.SetSelectedGameObject(null);
    Make sure you have this:

    Code (CSharp):
    1. using UnityEngine.EventSystems;
    Hope this helped.
     
    Edy and Sigma266 like this.
  25. kaarthickiyer

    kaarthickiyer

    Joined:
    Jun 28, 2017
    Posts:
    1
    I think it is a lot more simpler to implement with this code :
    public InputField mainInputField;

    // Activate the main input field when the scene starts.
    void Start()
    {
    mainInputField.ActivateInputField();
    }
     
  26. v01pe_

    v01pe_

    Joined:
    Mar 25, 2015
    Posts:
    49
    Note, that when you want to focus the InputField from an OnEnable callback (form a different Component), it can happen, that the input field did not yet run it's enable code and all above may not work.

    For me simply calling inputField.Select() did the trick, but I had to delay the call one frame with a coroutine!
     
    Ziplock9000 and hhoffren like this.
  27. hhoffren

    hhoffren

    Joined:
    Dec 16, 2012
    Posts:
    1
    Thank you for leading me to this direction! I tried to find a solution for Steam VR's UI Element button, under which I changed the text field to input field. Couldn't find nothing and even your one frame delay didn't work as controller messes something with the focus when the button / input field is hovered. I finally got this working by instructing player to move the hand away from input field after it was clicked and only then activating the input field with inputField.Select(). *phew*
     
  28. Chefty

    Chefty

    Joined:
    Jun 17, 2015
    Posts:
    24
    Hi,

    I'm working on android devices and I'd like to programmatically "select" the InputField so the TouchScreenKeyboard automatically show up. Does anyone has managed to do such a thing ?

    I'm not really sure what is the difference between OnFocus/OnSelect.

    I've been trying all the solution above and none work for what i'm trying to do. The keyboard never shows up until I manually tap on the input field.

    Thanks in advance,
     
  29. Chefty

    Chefty

    Joined:
    Jun 17, 2015
    Posts:
    24
    I finally found out a workaround.
    Apparently I was calling inputfield.Select() too soon. I made a coroutine to select the inputField at the right time and it worked.

    Code (CSharp):
    1. IEnumerator SelectInputField()
    2. {
    3.       yield return new WaitForEndOfFrame();
    4.       keyboardInputField.Select();
    5. }
    Hope it can help someone else,
    Cheers
     
    Ziplock9000 likes this.
  30. ramkesh

    ramkesh

    Joined:
    May 15, 2019
    Posts:
    9
    How to move Cursor from 1 Inputfield to 2 Inputfield in Unity? | Inputfield On Change Property. you can use Input TextField property OnChanged Value. From there after filling one text box Curson can focus where you want. For reference follow.
     
  31. Hassanshin

    Hassanshin

    Joined:
    May 8, 2017
    Posts:
    2
    NEVER DO THIS
    Code (CSharp):
    1. Input.Field.gameobject.setActive(false);
    2. // never do this
    try to disable the Image component, or change the alpha to 0