Hello,
we are encountering a very strange bug since we upgraded to Unity 2022.2.6f1 (from 2022.1.24f1) on Windows in the UI Elements system:
When a TextField is clicked in our UI for the first time, the first key press is ignored by the text field, only the second and further keypresses are actually registered and change the contents of the field.
Debugging this, we tracked this down to be caused by using textField.Focus() - which we kind of need for our UI to work as intended.
However, searching on google, the forums and the known issues list we could not find anyone reporting an even remotely similar issue.
Is there a bug report for this already, are we using something wrong, or has genuinely no one else encountered this yet?
Of course we tested that this is not caused by our own code doing something wrong, but no, it happens in a minimal reproduction project without any of our custom code as well.
I have attached the minimal repro.
Using that project the bug can be observed using these steps:
- Load project and open the “FocusRepro” scene directly in the Assets folder
- Start play mode
A: - The script in the scene automatically focuses the first text field in Start(), so just press any key now, watch it get ignored and then press again to finally see input show up.
B:
4) Un-focus the text field, Click the button in the scene
5) Type something. To make things even weirder, it works fine in this case (the Focus() call comes from the onClick event of the button)
Something we noticed in the UI Event debugger when trying to figure out what is going on is that the first key-press event is not sent to the text field, but to the UiDocument’s root element:
For completeness sake i am putting the code of the Start() method here aswell:
void Start() {
uiDocument = GetComponent<UIDocument>();
var textInput = uiDocument.rootVisualElement.Q<TextField>("CustomInput");
var button = uiDocument.rootVisualElement.Q<Button>();
textInput.RegisterCallback<ChangeEvent<string>>(
changed => {
Debug.Log($"Got change event : {changed.newValue}");
});
//!!! This breaks inputs
textInput.Focus();
button.clicked += () =>
{
Debug.Log("Clicked button");
//!!! But somehow it works as expected from here?
textInput.Focus();
};
}
8986825–1236973–FocusBugRepro.zip (28.4 KB)