Dsiable Tab in TextArea

Hi,

I’ve been reading about this a lot, but I can’t seem to find any solution. I’m doing a quick editor extension, and the user needs to be able to make a tab in the textarea. However, unity cycles through all textfields with tab. I’m looking for a way to disable that. As far as I’ve read, you can’t. This:

if (Event.current.keyCode == KeyCode.Tab)
{           
     Event.current.Use();
}

does catch the event, but doesn’t prevent it. I can set back focus manually though. My idea was to insert a tab at the cursor position, but I can’t seem to get that either. I’m using the following code:

        GUI.SetNextControlName("TextArea");
        string newValue = EditorGUILayout.TextArea(p.stringValue, GUILayout.MinHeight(50.0f));

        TextEditor editor = (TextEditor)GUIUtility.GetStateObject(typeof(TextEditor), GUIUtility.keyboardControl);

        if (Event.current.keyCode == KeyCode.Tab)
        {
            GUI.FocusControl("TextArea");
            newValue.Insert(editor.pos, "\t");
            editor.SelectNone();

            Event.current.Use();
        }

Editor.pos is always 0 though :frowning:

Any help would be greatly appreciated!

Is this just not possible?

I geuss that’s a yes, to bad

Actually, it is possible, from How to disable the Tab key event?

if (Event.current.keyCode == KeyCode.Tab || Event.current.character == '\t')
        Event.current.Use();
1 Like

Thanks for registering a new account to necropost a 6 year old post and answer a question that’s relevant only to obsolete versions of Unity UI’s. /s

3 Likes

Always a pleasure!
Thank you for your post, it is really a quality one and brings lot of value to all of us.

And actually, I posted it because I had problems with capturing tab in Unity 2018.3… It was relevant to me, and could be relevant to somebody else (not you, obviously).

4 Likes

And thanks for warm welcome to community!

3 Likes