Keyboard input issue for Dropdown in ETO

We wanted to use certain keyboard inputs to increment or decrement the dropdown index.
However, whenever we press the specific key/s, the dropdow items gets cleared.
We are assuming since dropdown uses keys to autocomplete, it might be causing the issue.
Below is a sample code.
Kindly guide how we can still use keyboard inputs for increment & decrement

public static DropDown CreateDropdown(Size size, IEnumerable<string> items)
{
    var dropDown = new DropDown
    {
        Size = size,
        TextColor = Colors.Gold,
        Font = new Font(SystemFont.Bold, 10),
        BackgroundColor = new Color(36f / 255f, 36f / 255f, 36f / 255f),
        DataStore = items.ToArray(),
    };
    return dropDown;
}
myDropdown = ETOUtils.CreateDropdown(new Size(90, 25), new List<string>() {"A","B","C","D" });
private void RhinoApp_KeyboardEvent(int key)
{
    if (!IsKeyDown(key))
        return;
    
    Application.Instance.Invoke(() =>
    {
        if (key == VK_Q)
        {
            if (myDropdown.SelectedIndex < myDropdown.Items.Count - 1)
                myDropdown.SelectedIndex += 1;
        }
        else if (key == VK_E)
        {
            if (myDropdown.SelectedIndex > 0)
                myDropdown.SelectedIndex -= 1;
        }
    });
}
[DllImport("user32.dll")]
private static extern short GetAsyncKeyState(int vKey);
private bool IsKeyDown(int key)
{
    return (GetAsyncKeyState(key) & 0x8000) != 0;
}

@stevebaer , @dale

I think you should be able to subscribe to the DropDown.KeyDown event, instead of using a keyboard event, then set the event argument’s .Handled property to true. This way the key down event is “consumed” by your event handler and won’t propagate further.

    myDropdown.KeyDown += OnKeyDown;    

    private void OnKeyDown(object sender, KeyEventArgs e)
    {
      // call the index increment/decrement code

      // set handled to true: this way the key press event stops here
      e.Handled = true;
    }

Added bonus: this is cross-platform, and your use of user32.dll is not.

1 Like

We tried .KeyDown event and set .Handled to True, but the same issue persists.

Also, keyboard event of Rhino fires twice on Key press. So to avoid that, we had added user32.dll.

Hi @maya_puundale,

Please review the attached sample.

TestMaya.cs (2.5 KB)

Let me know if you have any questions.

– Dale

1 Like

Thank you. This resolved the issue.
We realized that while populating the drop down with our items we were using DropDown.Datastore = new List items and this was causing the issue.
But instead using a for loop to add the strings one by one did not cause a issue.