- Home /
Keep InputField focus / selected when clicking a button / touch screen /UI 4.6
I am programming an ingame 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 does´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()*/
inputField.Select(); // I also tried to use this EventSystem.current.SetSelectedGameObject(go);
inputField.ActivateInputField();
inputField.ProcessEvent(Event.KeyboardEvent("a"));
still nothing on this question? I'm experiencing a similar issue.
Answer by bstevens · Jan 24, 2017 at 10:24 AM
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 ();
}
}
@bstevens Great helpfull for me. I can now input characters via custom buttons, and it works well but the character '#'. I can not input '#' by 'inputfiled.ProcessEvent(Event.$$anonymous$$eyboardEvent("#"))'. It says '#' = Shift in unity document, i wonder how to input the symbol '#' using keyboardevent, any help?Thanks.
Answer by nitry33 · Apr 17, 2015 at 09:59 AM
You solved the issue? Another question is about inputField used, how you close the unity touchscreenkeyboard for displaying yours?
U should use the comment if you not answering the question.
Your answer
Follow this Question
Related Questions
Make android number pad keyboard visible for TMPro_InputField 0 Answers
TouchScreenKeyboard Issue - iOS (iPad) 0 Answers
Cannot Hide Android keyboard when inputfield activated 1 Answer
[tvOS] UI.InputField doesn't show touchscreenKeyboard automatically 1 Answer
InputField instance isFocused always return false. 0 Answers