Remove focus from textfield

I know this has been asked a few hundred times, but unfortunately the answers seem to be deprecated now.

What i’m looking for is fairly simple, I have a label display the name of an object. In the game said object is re-nameable, so clicking on the label will change it to a textfield. But I want the user to be able to press enter (return) to leave the textfield, currently you can only do this by clicking off the textfield.

I have seen references to eatkeypressInTextField or some such thing, but it is deprecated now, and unity doesn’t even seem to recognize it.

How can i make it so that either pressing enter OR clicking off the textfield will take focus away from the textfield. (I will then change it from a textfield back to a label to make it clear).

after discovering that you can use gui.focuscontrol to set focus to null i solved this =) wasn’t aware that was possible.

if (GUILayout.Button(“Remove textfield focus”))

if you are working with editor try ‘EditorGUI.FocusTextInControl(null);’

-notice, only works during OnGUI(), maybe during OnInspectorGUI()


Don’t forget that you can remove a focus from your control while still inside the GUI function, then do something important from EditorApplication.update delegate. For example, deleting one of your entities, without messing up the order of drawing those GUI input fields. Also recall, GUI function is called multiple times in the same frame.