Search Unity

  1. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  2. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  3. Participate with students all over the world and build projects to teach people. Join now!
    Dismiss Notice
  4. Build games and experiences that can load instantly and without install. Explore the Project Tiny Preview today!
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  6. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Give Dropdown List capture / control of the arrow keys

Discussion in 'Unity UI & TextMesh Pro' started by cheezorg, Apr 8, 2016.

  1. cheezorg

    cheezorg

    Joined:
    Jun 5, 2008
    Posts:
    389
    Sorry if this is a duplicate, been searching all afternoon for a solution.

    I'm trying to navigate a UI screen without the mouse (keyboard only). My UI screen consists of a few input fields and two dropdown lists.

    I added the ability to tab between input fields and the dropdown lists by calling OnPointerClick from code. So far so good.

    The problem is that once I've tabbed over to a dropdown list, I can't actually SELECT anything in the dropdown list without ALSO bringing the mouse cursor to hover over the list. Once I do that - BOOM! - it magically works and the arrow keys are captured by the Dropdown as expected.

    Tabbing to the Dropdown expands the list (and looks like it's active) - but it's not really active. If I try to arrow down through the list the UI just navigates down to the next input field instead of scrolling through my Dropdown List.

    What I need to figure out is how to actually select the Dropdown list, without using the mouse to hover over it.

    OnPointerClick is obviously doing something, since it's expanding the list, but what else do I need to call to actually activate it so that the arrow keys will scroll through the list and let me select a choice?
     
    Last edited: Apr 8, 2016
  2. guneyozsan

    guneyozsan

    Joined:
    Feb 1, 2012
    Posts:
    52
    Necroing this since I couldn't find something about the problem other than this topic. For anyone still looking for a solution I handled this by iterating Toggles in children of Dropdown game object:

    Code (CSharp):
    1. // Display dropdown options.
    2. dropdown.OnPointerClick(new PointerEventData(system));
    3.  
    4. Toggle[] toggles = dropdown.GetComponentsInChildren<Toggle>();
    5. const string DropdownListItemPrefix = "Item ";
    6.  
    7. // Find the first item on list and select it so that arrow keys navigate between dropdown options.
    8. foreach (Toggle toggle in toggles)
    9. {
    10.     if (toggle.gameObject.name.Contains(DropdownListItemPrefix))
    11.     {
    12.         toggle.OnPointerEnter(new PointerEventData(system));
    13.         system.SetSelectedGameObject(toggle.gameObject);
    14.         return;
    15.     }
    16. }