1

I have a unity scene which has 4 TMP_FieldInput's on it. (I am also using the new Unity Input system if it matters). What I'm trying to accomplish is to allow tabbing between the fields. The solution I have so far "sort of" works, but not correctly. When I press tab, I see the carat move to the proper InputField and start flashing as if it's ready for input. But when I start typing, nothing happens. The only way I can enter text into any of the 4 InputField's is to click on them first.

I've been looking for solutions, and have come across all kinds of suggestions, but nothing seems to work. No matter what, the carat moves, but I can't type until I click first.

FYI: Unity v. 2022.3.4f1 TMP: v. 3.0.6

Here is my current code:

using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.InputSystem;

public class InputFieldManager : MonoBehaviour
{
    public TMP_InputField inputField1;
    public TMP_InputField inputField2;
    public TMP_InputField inputField3;
    public TMP_InputField inputField4;

    public int inputSelected = 1;

    private void Awake()
    {
        inputField1.Select();
    }

    private void Update()
    {
        if (Keyboard.current.tabKey.wasPressedThisFrame && Keyboard.current.leftShiftKey.wasPressedThisFrame)
        {
            inputSelected--;
            if (inputSelected < 1) inputSelected = 4;
            SelectInputField();
        }
        else if (Keyboard.current.tabKey.wasPressedThisFrame)
        {
            Debug.Log("TAB!");
            inputSelected++;
            if (inputSelected > 4) inputSelected = 1;
            SelectInputField();
        }
    }

    private void SelectInputField()
    {
        switch (inputSelected)
        {
            case 1:
                inputField1.Select();
                break;
            case 2:
                inputField2.Select();
                break;
            case 3:
                inputField3.Select();

                break;
            case 4:
                inputField4.Select();

                break;
        }
    }
}
Todd Davis
  • 5,855
  • 10
  • 53
  • 89

1 Answers1

0

Select is indeed only selecting the field as the currently focused UI item (have the tab functionality of a web browser in mind) and you would usually hit Enter in order to activate it from there.

You probably want to additionally/instead call ActivateInputField

// imho a bit nicer to read/maintain and reduces redundant implementations
var inputField = inputSelected switch
{
    1 => inputField1,
    2 => inputField2,
    3 => inputField3,
    4 => inputField4,
    _ => null
};
    
if(inputField != null)
{
   inputField.ActivateInputField(); 
}

Further little refactoring suggestion: I would use an array to be more flexible and go 0 index based and skip the switch. And hen you can reduce some duplicate lines and checks

public TMP_InputField[] inputs;
public int inputSelected;

private void OnEnable()
{
    SelectInputField();
}

private void Update()
{
    if (Keyboard.current.tabKey.wasPressedThisFrame)
    {
        if(Keyboard.current.leftShiftKey.wasPressedThisFrame)
        {
            inputSelected--;
            if (inputSelected < 0) inputSelected = inputs.Length - 1;
        }
        else
        {
            inputSelected = (inputSelected + 1) % inputs.Length;
        }

        SelectInputField();
    }
}

private void SelectInputField()
{
    var inputField = inputs[inputSelected];
    inputField.ActivateInputField();
}
derHugo
  • 83,094
  • 9
  • 75
  • 115
  • Thank you derHugo, I appreciate the response and the refactor. Sadly, it still does the same thing. What I noticed now is that when I press tab and see the carat move to the next field, and then I type something, then my input is going to the EDITOR. In other words, if I type a word in the first field, then press tab, and then press the letter "F", I see my IDE zoom in and focus on the field, rather than entering "F" in the input field. – Todd Davis Jul 10 '23 at 15:16
  • One other thing I should mention - I have the new Unity Input system enabled, not sure if that has anything to do with it? It was driving me insane trying to get Input.GetKeyDown() working until I realized that the new Input system doesn't work with it, which is why I went with Keyboard.current... Can that affect this as well? – Todd Davis Jul 10 '23 at 15:17
  • @ToddDavis You can change your project player settings to support both Input systems at the same time if you rather want to stick to `Input.GetKeyDown` - shouldn't make a difference regarding the behavior though afaik ... it sounds though indeed as if the tab press actually makes your Unity editor itself switch focus out of the GameView onto the SceneView which seems weird – derHugo Jul 10 '23 at 15:28
  • What if you build the game/scene? – Obscure021 Jul 10 '23 at 15:29
  • @Obscure021 depends .. you might get an issue if it is e.g. WebGL not sure .. usually I would expect (like you) the player handle input first before letting it bubble out of the GameView/Player – derHugo Jul 10 '23 at 15:33
  • @ToddDavis only tested it with `GetKeyDown` tbh and that woks for me in the editor just fine – derHugo Jul 10 '23 at 15:34
  • @Obscure021 Thanks for that suggestion. It DOES work when built, which is good to know. Now I just need to figure out why it's wonky in the editor. – Todd Davis Jul 10 '23 at 15:40
  • @derHugo - Thanks, I'll try the GetKeyDown again as you suggested – Todd Davis Jul 10 '23 at 15:41
  • @ToddDavis Try editing the preferences. There should be something that could help. – Obscure021 Jul 10 '23 at 15:49
  • @ToddDavis What is your Unity Version? – Obscure021 Jul 10 '23 at 15:56
  • FYI: Unity v. 2022.3.4f1 TMP: v. 3.0.6 – Todd Davis Jul 11 '23 at 15:07