- Home /
How to tell if any textField has keyboard focus in editor?
Hi,
I want to do shortcut keys in my editor, and I don't want it be triggered when we do typing in some TextField.
Although there's EditorGUIUtility.editingTextField, but this flag stays true even when you clicked outside the TextField, so this gives a unsatisfying result.
Thanks in advance,
Hmm, though I cannot find out the most appropriate method to check the focus state, I do find a temporary workaround:
Just put this snippet at the start part in the OnGUI() method
if (e.type == EventType.$$anonymous$$ouseDown && EditorWindow.focusedWindow == this)
EditorGUIUtility.editingTextField = false;
then I could do the focus check with EditorGUIUtility.editingTextField again.
@T$$anonymous$$Pxyz See my answer below -- this does work as expected without the need for a big "if" statement. This handles input correctly as long as you put it in, say, an OnGUI event inside an editor window.
Answer by awesomedata · Apr 18, 2018 at 06:50 PM
How are you checking the Debug.Log()
and are you doing this in the Editor itself (i.e. EditorWindow)? -- If you are doing this in, say, an OnGUI event or in a custom Update method via delegate, then many times, the Debug.Log()
does not change without mouse movement, and even as such, this depends on where you check and what else is taking up mouse input and then calling Use()
on it. This could explain why the Debug.Log()
is not updating.
That being said, the function EditorGUIUtility.editingTextField
should only be called in the OnGUI event anyhow, and if all that is done correctly, it does handle input fields such as the searchbox in the project window (and other edit fields) correctly.
Answer by praschl · Oct 31, 2021 at 01:23 PM
Ok this may be a bit old, but here the reason, EditorGUIUtility.editingTextField stays TRUE could be, that just clicking outside of a textfield doesn't necessarily make the focus of the textfield go away, it can stay focused. RETURN will remove the focus from the textbox. ESC will remove the focus from the textbox with undoing the edit. TAB will sometimes also remove the focus, depending on whats the next focusable ui element.
On the other hand, if you have the issue of EditorGUIUtility.editingTextField staying FALSE, even when the textbox is focused, try using EditorGUILayout.TextField instead of GUILayout.TextField, that solved my issue.