Tab between input fields

I have a simple login form with email and password input fields next to each other.
Now I set up 2 input axes called UI Horizontal and UI Vertical and set them as navigation axes in the Event System. The UI Vertical includes Down Arrow as well as Tab key.

While the UI navigation seems to work with buttons it doesn’t seem to work with input fields.
When I press Up, Down or Tab on a single-line input field it doesn’t switch the focus.

How could I achieve the expected behavior?
Is this a bug with input fields?

2 Likes

Hi, we don’t support this currently, but the functionality should be added, please file a bug.

1 Like

I use this temporary workaround (add on one object, will work for all GUI elements in the scene):

using UnityEngine.UI;
using UnityEngine.EventSystems;
public void Update()
{
 
if (Input.GetKeyDown(KeyCode.Tab))
{
    Selectable next = system.currentSelectedObject.GetComponent<Selectable>().FindSelectableOnDown();

    if (next!= null) {
                       
        InputField inputfield = next.GetComponent<InputField>();
        if (inputfield !=null) inputfield.OnPointerClick(new PointerEventData(system));  //if it's an input field, also set the text caret
                       
        system.SetSelectedGameObject(next.gameObject, new BaseEventData(system));
    }
    //else Debug.Log("next nagivation element not found");
   
}
}

updated for it to work with all navigation types

15 Likes

This didn’t work alone, needed to add:

EventSystem system;

    void Start ()
    {
        system = EventSystemManager.currentSystem;
          
    }
5 Likes

Did this feature ever get added?

From what I can tell inputfields are buggy as hell an I can’t get a hold of a developer to address the issue.

Bug reports for bugs speak louder the forum posts.

No it has not yet.

1 Like

Here’s what I’ve observed…

If you have clicked on an InputField, it will not only have the control focus, but will be in edit mode. If you press the enter key on your keyboard, that will take that InputField out of edit mode. When not in edit mode, the standard control navigation system (arrow keys) works. So I would not actually consider this a bug. Meaning, it appears to be working as designed.

Having said that, we do need a way to cycle between InputField controls using the tab key. I added a feature request in the feedback section as the built-in bug reporter specifically says if it’s a new feature request, use the feedback system instead of the bug reporter. Will that suffice, or should I be calling this a bug?

http://feedback.unity3d.com/suggestions/new-unity-ui-4-dot-6-should-support-tab-key-based-navigation-between-input-fields

3 Likes

I believe this is already added to feedback http://feedback.unity3d.com/suggestions/ugui-input-field-navigation. That’s from last July, so unfortunately there is no rush to implement it.

Glad this thread was around. I don’t understand why this wasn’t built-in; moreso, I don’t understand why it hasn’t been implemented yet.

The snippet above worked wonderfully. Many thanks to posting this.

2 Likes

Worked perfectly for me. Just had to change some code to some updated functions and variables:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class InputNavigator : MonoBehaviour
{
    EventSystem system;
  
    void Start()
    {
        system = EventSystem.current;// EventSystemManager.currentSystem;
      
    }
    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Tab))
        {
            Selectable next = system.currentSelectedGameObject.GetComponent<Selectable>().FindSelectableOnDown();
          
            if (next != null)
            {
              
                InputField inputfield = next.GetComponent<InputField>();
                if (inputfield != null)
                    inputfield.OnPointerClick(new PointerEventData(system));  //if it's an input field, also set the text caret
              
                system.SetSelectedGameObject(next.gameObject, new BaseEventData(system));
            }
            //else Debug.Log("next nagivation element not found");
          
        }
    }
}
25 Likes

I used this and it worked great for ONE set of input fields (I enable and disable them according to the state of the game). When I added this to another script for another set of input fields, it broke for both. Anyone have a clue what I can do to have this in more than one script and have it still work? Only one of the scripts will be enabled at a time.

Disable the interactable setting on each input field you’re not using (a pain) or use a canvas group to disable them entirely when not in use. Just use one tab manager script. Set each input’s navigation as you desire. Use the graphical view for better reference, so you can keep closed groups of navigation sets.

Just use one tab manager script. It works great.

1 Like

Works great! This should be default on the input navigation options because its such a common thing to need.

Hi,

if you also want to make the tab cycle between elements and go back by pressing shift, replace:

            Selectable next = system.currentSelectedGameObject.GetComponent<Selectable>().FindSelectableOnDown();

by

      Selectable next = null;
       if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
       {
        next = system.currentSelectedGameObject.GetComponent<Selectable>().FindSelectableOnUp();
         if (next==null)
           next = system.lastSelectedGameObject.GetComponent<Selectable>();
       }
       else
       {
         next = system.currentSelectedGameObject.GetComponent<Selectable>().FindSelectableOnDown();
         if (next==null)
           next = system.firstSelectedGameObject.GetComponent<Selectable>();
       }
3 Likes

I’m actually getting a strange behavior when using it with the Bolt networking debug console overlay on of it. As i toggle through the input controls the debug console disappears then appears.

Yeah, Bolt console display default button is tab. Go into the Bolt Settings window, change the default button to something that isn’t tab.

I am trying with Android mobile and lot’s of issues happen with input text field

  1. On switching between two text input field, text will disappear.
  2. on typing text iin another input field , text getting typed will appear in all the input fields

I think those particular input field bugs with text from one input field showing up in another have recently been fixed in a patch. 4.6.6p3 maybe? Not sure about the Unity 5 though.

Still seeing this in 5.0.0f4

The text that disappears I believe is placeholder text. At first I thought it was something to do with the fullscreen mode contracting to fit the software buttons, and causing the text to truncate, but I’ve set it to best fit and it still happens so I’m not sure.

The copied text issue is intermittent and I haven’t been able to reliably reproduce but seems to be actually putting the text into all fields, bypassing validation. Which is a real problem if that’s true. Edit I don’t think it actually bypassing validation. Just a display issue.