Focus on InputField upon key press?

Basically the title, I’m trying to focus on an Input Field, when a player presses a key.

I’m using this for a chatbox, so I want it when someone presses ENTER or Y, the chat will pop up, and focus so they can type, then they can press ENTER again to send it. I’ve got the chat box working already, just wondering how I can setup the focusing and what not.

I’m currently using a work around. Here’s my script.

    using UnityEngine;
    using UnityEngine.UI;
    using UnityEngine.EventSystems;
     
    public class Chat : BoltCallbacks
    {
        public Scrollbar scroll;
     
        public RectTransform content;
        public Text chatLine;
        public InputField input;
     
        public override bool PersistBetweenStartupAndShutdown ()
        {
            return true;
        }
     
        void Awake ()
        {
            input.onSubmit.AddListener (SendChat);
        }
     
        void Update ()
        {
            scroll.value = + -0.01f * Time.deltaTime;
     
            if (Input.GetKeyDown(KeyCode.Y))
            {
                input.gameObject.SetActive(true);
                EventSystemManager.currentSystem.SetSelectedGameObject(input.gameObject, null);
                input.OnPointerClick (null);
            }
        }
       
        void SendChat (string line)
        {
            input.value = "";
            input.gameObject.SetActive (false);
     
            if (line == "")
            {
                return;
            }
     
            IChat evnt = BoltFactory.NewEvent<IChat>();
            evnt.text = PlayerNamePlate.playerName + ": " + line;
            BoltNetwork.Raise(evnt);  
        }
     
        public override void OnEvent (IChat evnt, BoltConnection cn)
        {
            Text newLine = ((GameObject) Instantiate(chatLine.gameObject)).GetComponent<Text>();
            newLine.gameObject.SetActive(true);
            newLine.rectTransform.parent = content;
            newLine.text = evnt.text;
        }
    }

The problem now is, whenever I open chat with “Y”, the chatbox actually types Y… I’ve tried a few things, nothing seems to stop from typing the first Y, which is just suppose to open the chat.

If you use Input.GetKeyUp() instead it’ll avoid that. It’s usually the more appropriate check to use for one-off actions anyway.

If it adds an extra y every time, it might be possible to manually remove it every time, or at the very least set the text to “” once before the player can hit a second key.

I actually found this while looking for a way to make a function manually select/focus an InputField fully, where the next key entered is written to the field. I’ve managed to use SetSelectedGameObject to set the InputField as selected, but this was only the state that allowed for keyboard navigation to move between different UI elements. From your post it seems I also need

inputField.OnPointerClick (null);

Thanks for the help, and good luck with your project.

Try using WaitForSeconds()
For example:

if (Input.GetKeyUp(KeyCode.Y))
            {

                yield return new WaitForSeconds(1);
                input.gameObject.SetActive(true);
                EventSystemManager.currentSystem.SetSelectedGameObject(input.gameObject, null);
                input.OnPointerClick (null);
            }