Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Disabling the Tab Focus System

Discussion in 'UI Toolkit' started by BIIDanielGoetz, May 31, 2022.

  1. BIIDanielGoetz

    BIIDanielGoetz

    Joined:
    Jul 3, 2021
    Posts:
    4
    Hello everyone,

    in our project we are using Unity 2022.1.1f1 as well as the UI Toolkit. However, we don't want the built-in functionality of switching focus between different elements by pressing Tab, as this Key is used for something else. How can we easily disable the tab functionality for all our UI?

    I know that setting the tabindex of focusable Visual Elements to -1 prevents them from being focused when tab is pressed, but for us, it is not feasible to manually change this in all current and future uxml files. Is there a more simple way to disable this functionality?

    Furthermore, before we upgraded to Unity 2022.1.1f1 from 2021.2.2f1 we didn't have this problem, as pressing tab didn't seem to focus TextFields and Scrollers as it does now. Has there been a change to the focusing logic, that I'm not aware of?
     
  2. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    700
    I have reported this as a bug (1398890) but they closed the ticket and said this:

     
    _geo__ likes this.
  3. BIIDanielGoetz

    BIIDanielGoetz

    Joined:
    Jul 3, 2021
    Posts:
    4
    Thanks for the reply, unfortunately, the solution offered by Unity does not work in my case.
    On the one hand, the Direction.Previous and Direction.Next Enum values do no exist, meaning the code does not compile. But even when I remove the direction check and stop the propagation of all events, the code does not help.

    Regardless, it is a bit disheartening that a solution like that is even required. I'd be very happy if there was a better solution / working solution for this problem.
     
  4. BIIDanielGoetz

    BIIDanielGoetz

    Joined:
    Jul 3, 2021
    Posts:
    4
    Update: I was able to "disable" the tab focus system by putting the following code into the update loop. This works for our use case, but is obviously just a dirty workaround.

    Code (CSharp):
    1. if (UnityEngine.Input.GetKey(KeyCode.Tab)) {
    2.   // The workaround basically un-focuses anything that unity could have focused when tab is pressed
    3.   _document.rootVisualElement.focusController.focusedElement?.Blur();
    4. }
     
  5. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    700
    You don't have to do this manually. You could let a python/php/perl script do that for you, or you can do it in the C# code like so:

    Code (CSharp):
    1.     public static void disableTAB(VisualElement el)
    2.     {
    3.         el.tabIndex = -1;
    4.  
    5.         foreach (var child in el.Children())
    6.         {
    7.             disableTAB(child);
    8.         }
    9.     }
    10.  
     
  6. BIIDanielGoetz

    BIIDanielGoetz

    Joined:
    Jul 3, 2021
    Posts:
    4
    Thanks for the suggestions. I thought of a solution like that as well, but I presumed that it wouldn't work when TemplateContainers with TextFields or TextFields themselves are instantiated and added during runtime.

    When I tested a similar snippet of code in my Start() method, it didn't change anything, probably because of the aforementioned reason: Most of our UI is created during runtime and changes quite often.
     
  7. MousePods

    MousePods

    Joined:
    Jul 19, 2012
    Posts:
    759
    Should work.
    Code (CSharp):
    1. private IEnumerator Start()
    2. {
    3.  
    4. yield return new WaitForEndOfFrame();
    5.  
    6. // code here
    7. }
     
  8. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    700
    You can also try calling this on GeometryChange event.