Removing focus from a TMP_InputField
So here's the deal--I have an editor in my game with multiple windows that each contain numerous TextMeshPro input fields. Having to manually click through each input field is slow, and so I have my Update() loop listening for Input.GetKeyUp( KeyCode.Tab ), and then--when it detects that the Tab key has been released after a press--it calls a function that cycles focus to the next input field in the current window using the ActivateInputField() method. Simple enough, right?
Well, here's the problem: when I call ActivateInputField() on a new TMP_InputField, the prior TMP_InputField also stays active, displaying a blinking cursor. I tried addressing this by calling DeactivateInputField() on the prior inputfields, but no dice--if I tab through all the TMP_InputFields in the window, I'll end up with blinking cursors in all of them.
I assume the problem is that I shouldn't be using ActivateInputField() to give focus to a new TMP_InputField--but if not, then what?
$$anonymous$$ore info: after a bit more Googling, I suspect the issue is related to this behavior: https://forum.unity.com/threads/reset-on-deactivation-leaves-blinking-caret.535826/
The blinking caret remains in a previously active T$$anonymous$$P_Inputfield after I've activated a different T$$anonymous$$P_Inputfield--and indeed, even after I specifically deactivate the previous T$$anonymous$$P_Inputfield. Is this a bug, or am I doing something wrong?
Answer by loljoch · Apr 26, 2020 at 11:37 AM
For anyone stumbling upon this thread like I did, I have not found the legit way to do it, but I have found a loophole. Basically if you want to lose focus from your TMP_InputField you can just use
if (Input.GetKeyDown(KeyCode.Tab))
{
inputField.interactable = false;
inputField.interactable = true;
}
If anyone has a better solution, please tag me!
Answer by Sinister-Design · Jun 30, 2019 at 07:21 PM
Can I get some guidance from a Unity developer on this? Is this a known bug?
Answer by Arthur-LVGameDev · Nov 12, 2020 at 03:31 AM
Another year later, never found the answer online, but ended up finding that I had figured it out within our codebase a few years back.
Here's what works for us as of now, heh:
searchInput.text = "";
searchInput.DeactivateInputField();
Answer by DeanShea · Oct 17, 2021 at 07:02 AM
I've found EventSystem.current.SetSelectedGameObject(null)
as a way to deselect an input field. It runs OnDeselect()
on the input field and does everything as expected (no carat and pressing enter doesn't reselect it). For the original question you could play around with EventSystem.current.SetSelectedGameObject(input_field_2)
Your answer
Follow this Question
Related Questions
Unity 5.3 Caret is missing on mobile devices 0 Answers
How do I let users change the background color of a selected word in an input field? 0 Answers
InputField not able to hold long text 2 Answers
How to restrict user from typing certain numbers in input field 3 Answers
How to change caret position inside Input field using mouse position 0 Answers