I’ve got several user interfaces that require text entry in our application - but we also use the WSAD keys (among others) to control movement and other actions within the game.
Like many, I discovered that the UI controls don’t consume the Input messages - code that is waiting for navigation or action key presses will still run, regardless of the fact that the UI control had focus and has used that key to type into an InputField.
I went looking for solutions, but couldn’t find one that seemed simple enough, so I thought I’d share my current script…
public static bool IsUIElementActive()
{
if (EventSystem.current.currentSelectedGameObject != null)
{
UnityEngine.UI.InputField IF = EventSystem.current.currentSelectedGameObject.GetComponent<UnityEngine.UI.InputField>();
if (IF != null)
{
return true;
}
}
return false;
}
It quite simply checks to see if there is a selected UI item, and if that item happens to be a InputField, it returns ‘true’. This could be extended to suit other controls as well, but by calling IsUIElementActive before we do any Input processing, we can quicky check to ensure that the user isn’t currently typing anything in to a text box.