Focus on InputField Programmatically

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?

2 Likes

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.

5 Likes

Thanks, Jmorr10!
Your solution worked for me.

1 Like

Hi. I am trying to do the same but my for some reason I get a NullReferenceException on the line:

input.OnPointerClick(null);

Here is the full code.

void Update()
{
    if (Input.GetKeyDown(KeyCode.A))
    {
        EventSystem.current.SetSelectedGameObject(input.gameObject, null);
        input.OnPointerClick(null);
    }
}

The first line works fine. I hope you can help me.

2 Likes

Are you using Beta 20? This method appears to have been broken / changed in the upgrade.

Yeah, this doesn’t work anymore in B20. I would love to find a new solution.

This seems to work now:

EventSystem.current.SetSelectedGameObject(inputField.gameObject, null);
inputField.OnPointerClick(new PointerEventData(EventSystem.current));
7 Likes

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)?

1 Like

I’d call inputfield.ActivateInputField() not OnPointerClick because OnPointerClick checks to see if the eventData.button is “PointerEventData.InputButton.Left”.

public virtual void OnPointerClick(PointerEventData eventData)
    {
        if (eventData.button != PointerEventData.InputButton.Left)
            return;

        ActivateInputField ();
    }
6 Likes

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.

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.

Hmm that doesnt seem right, could you report a bug please.

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.

2 Likes

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?

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.

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 =)

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.

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.

Yeah that works… just use:

inputField.ActivateInputField();
inputField.Select();

and change the the Content Type to “Multi Line Submit” or “Multi Line Newline”

2 Likes

void Update()
{
if (Input.GetKeyDown(KeyCode.A))
{
InputField input = this.GetComponent();

//方案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