- Home /
Assign a hotkey to a GUI button
I want to press "Enter" to take input from input field instead of clicking on submit button or anything like that and also I want that the next input field get selected automatically for input. I don't want to take input from both input fields at same time by pressing "Enter" only once i.e. first 1st input field will take input then I'll press Enter and next input field gets selected for input and then I'll put data and press Enter to get input from 2nd input field. PLEASE find solution for this Also where to add script for this? please help ! Thank You
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class Input1 : MonoBehaviour
{
public InputField name1;
public InputField name2;
public void get1()
{
fword1.text = name1.text + "'s word is '" + word1.text + "'";
}
public void get2()
{
fword2.text = name2.text + "'s word is '" + word2.text + "'";
}
}
Answer by WarmedxMints · Mar 04, 2019 at 06:59 PM
You can use Unity's built-in event system to check which gameobject is currently selected and then do something if return is pressed while it is.
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class Input1 : MonoBehaviour
{
public InputField name1;
public InputField name2;
private void Update()
{
if(Input.GetKeyDown(KeyCode.Return))
{
//If field one is selected
if(EventSystem.current.gameObject == name1.gameObject)
{
//Set field 2 active
EventSystem.current.SetSelectedGameObject(name2.gameObject);
}
//If enter is pressed when field2 is selected
if (EventSystem.current.gameObject == name2.gameObject)
{
//Whatever you wish to do when return is pressed on field 2
}
}
}
}
thanks for answer but where to add this script? I mean on Button? or where? because I was using OnClick Get() function previously to print data.
Just add the update method to your current Input1 script.