Keep InputField focus / selected when clicking a button / touch screen /UI 4.6

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:

inputField.ProcessEvent( Event.KeyboardEvent("a") );

where "a" represents the a key.

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:

You can then use something like this on a UI button to send keystrokes to the InputField. Note the call to inputField.ForceLabelUpdate():

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
  
public class TestKeyboardButton : MonoBehaviour {
  
	public FocusInputField inputField;
	private Button btn;
  
	void Start () {
		btn = GetComponent<Button> ();
		btn.onClick.AddListener (OnClick);
	}
	
	void OnClick () {
		inputField.ProcessEvent(Event.KeyboardEvent("a"));
		inputField.ForceLabelUpdate ();
	}
}

Unity still don’t have a good solution for this, so here’s a script that allows restoring input field focus state (including caret/selection state).

You solved the issue?
Another question is about inputField used, how you close the unity touchscreenkeyboard for displaying yours?