I am programming an in-game keyboard to be used in windows standalone. I want to use an InputField as the target for the user input.
The problem is the InputField lost focus when a button is clicked (in this case the button represents a keyboard key) and it doesn’t catch correctly the key that i send.
To simulate keyboard key pressed events I use this:
I tried using inputField.ActivateInputField(); and inputField.Select(); in different ways but it didn’t solve my problem.
Do anyone know how to solve this?
Thanks
Full code
// "a" BUTTON CLICK HANDLER , I also tried putting it inside Update()
// I also tried to use this EventSystem.current.SetSelectedGameObject(go);
inputField.Select();
inputField.ActivateInputField();
inputField.ProcessEvent( Event.KeyboardEvent("a") );
You should create a new script that inherits from Unity’s InputField class and overrides the OnSelect and OnDeselect methods. This will disable the default behaviour and maintain focus on the InputField:
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class FocusInputField : InputField {
public override void OnSelect(BaseEventData eventData)
{
Debug.Log ("Overrides InputField.OnSelect");
//base.OnSelect(eventData);
//ActivateInputField();
}
public override void OnDeselect(BaseEventData eventData)
{
Debug.Log ("Overrides InputField.Deselect");
//DeactivateInputField();
//base.OnDeselect(eventData);
}
}
Add a InputField UI element to the scene, remove the default InputField component and add your new class FocusInputField as a new component.
Remember to add the Text component and Placeholder references as these will be missing when you add the FocusInputField component.
The modified component should look like this in the inspector: