- Home /
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.
Answer by orb · Sep 09, 2014 at 08:10 PM
If you use Input.GetKeyUp() instead it'll avoid that. It's usually the more appropriate check to use for one-off actions anyway.
Answer by HaydenHuff · Sep 30, 2014 at 02:56 AM
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.
Answer by MIHGames · Sep 30, 2014 at 03:51 AM
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);
}
Your answer
Follow this Question
Related Questions
How do I make a "move pad" on the screen? (Mobile) 1 Answer
TextField, Event.current, Input.GetKey, and GUI.FocusControl locking 1 Answer
How can I release gui focus via code? 6 Answers
Why when set TouchScreenKeyboard.hideInput = true is not supported by any language besides English? 0 Answers
Defocus input line? 0 Answers