Search Unity

Resolved How can I select a dropdown item using arrow keys by script?

Discussion in 'UGUI & TextMesh Pro' started by hwk9120, Jan 15, 2024.

  1. hwk9120

    hwk9120

    Joined:
    May 9, 2023
    Posts:
    17
    Hi, I am making hotkeys to click selectable UI things like button, toggle etc.
    For this I wrote a script 'ShortcutManager' which has a list of pair (keycode and selectable)

    When I press one keycode registered in the script, a selectable component paired with the key code is clicked.
    By below code (ShortcutManager)
    Code (CSharp):
    1. ExecuteEvents.Execute(keyCodePair.selectableComponent.gameObject, new BaseEventData(EventSystem.current), ExecuteEvents.submitHandler);
    I want to click a dropdown item using arrow keys right after clicking dropdown.
    Also I want to deselect that dropdown using esc keys after clicking dropdown.
    But I can't.

    If I am right, that item must be in a state OnPointerEnter to select dropdown item with that keys.
    But I don't have any idea what to do.
     
  2. hwk9120

    hwk9120

    Joined:
    May 9, 2023
    Posts:
    17
    I solved this problem simply.
    Now I can use arrow keys to click my dropdown items :D

    Code (CSharp):
    1. ExecuteEvents.Execute(keyCodePair.selectableComponent.gameObject, pointer, ExecuteEvents.pointerDownHandler);
    2. ExecuteEvents.Execute(keyCodePair.selectableComponent.gameObject, pointer, ExecuteEvents.submitHandler);