- Home /
Found the reason
Adding Tab support to TextArea's TextEditor (almost done)
Hi,
I want my TextArea to support entering tabs. After a lot of search I finally came up with this:
Event ev = Event.current;
string mPlaintext = GUILayout.TextArea(myText, GUILayout.ExpandHeight(true));
TextEditor mEditor = (TextEditor)GUIUtility.GetStateObject(typeof(TextEditor), GUIUtility.keyboardControl);
if (GUIUtility.keyboardControl==mEditor.controlID && ev.Equals(Event.KeyboardEvent("tab"))) {
if (mPlaintext.Length > mEditor.pos) {
mPlaintext = mPlaintext.Insert(mEditor.pos, "\t");
mEditor.pos++;
mEditor.selectPos = mEditor.pos;
}
ev.Use();
}
myText = mPlaintext;
While this works (\\t is added to the text in the TextArea), ev.Use() seems to be ignored, so the default behaviour of tab kicks in and the next control is selected.
Anybody knows how I can eat the tab event to prevent control switching?
Edit: Seems to be a problem with GUILayout.TextArea. Using GUI.TextArea does the trick!
Thanks Jake
PS: Don't know if it matters, but the code is placed in EditorWindow.OnGUI()
Answer by Benproductions1 · Mar 26, 2013 at 01:11 AM
Hi! there,
What you probably want to do is force focus on the TextField. You can do this by using the following commands:
//Before you call the text field, give it a special identifier
GUI.SetNextControlName("1");
//You can then call the following to force focus on the text field with that identifier
GUI.FocusControl("1");
I have found this useful more often than not. If you have multiple text fields, you can keep track of them yourself, and only change the focus, when other exit events happen :)
Hope this helps, Benproductions1
What if there are 2 textfields next to each other, both having assigned such an ID. If you are focussing one, while the other gets clicked, how does changing focus work then with your method? You probably have to control if the other textArea gets clicked now?
Answer by Jake-L · Mar 26, 2013 at 08:41 AM
GUI.FocusControl() doesn't help, but I figured out that using GUI.TextArea instead of GUILayout.TextArea works. Thanks anyway!
Follow this Question
Related Questions
UnityEvent draws debug variables in the inspector 0 Answers
The type or namespace name 'EventSystems' does not exist in the namespace 'UnityEngine' 0 Answers
Mouse click in edit mode. ✱✺✸❁ 1 Answer
Detect Enter Key Event with GUILayout.TextField Focused 1 Answer
Detect mouse up or end of drag on any scene object 0 Answers