Search Unity

  1. Unity 2019.1 beta is now available.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We're looking for insight from anyone who has experience with game testing to help us better Unity. Take our survey here. If chosen to participate you'll be entered into a sweepstake to win an Amazon gift card.
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Unity 2018.3 is now released.
    Dismiss Notice
  6. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    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:
    57
    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. }