Search Unity

Give Dropdown List capture / control of the arrow keys

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

  1. cheezorg

    cheezorg

    Joined:
    Jun 5, 2008
    Posts:
    394
    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:
    99
    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. }