UWP AutoSuggestBox - Up/Down arrow key fires SuggestionChosen

Viewed 522

When the suggestion list is open, the up/down arrow keys automatically fires the SuggestionChosen event. I am wondering if there is a way to intercept this key press? I have tried to use the KeyDown and KeyUp events to catch these key presses but the SuggestionChosen event occurs before the KeyDown/Up events. This effectively forces the user to choose either the first or the last suggestion on the list. Mouse click or touch selection is fine.

I would just like to ignore the arrow keys while typing in the AutoSuggestBox. Or, not force the user to choose the first or last items with the arrow keys. Is there any way to accomplish this? Thank you

XAML

<AutoSuggestBox Name="EmailSuggestBox" 
                PlaceholderText="Email"
                Text="{Binding Customer.EmailAddress, Mode=TwoWay}"
                TextChanged="EmailSuggestBox_TextChanged"
                QuerySubmitted="EmailSuggestBox_QuerySubmitted"
                SuggestionChosen="EmailSuggestBox_SuggestionChosen"
                LostFocus="EmailSuggestBox_LostFocus"
                KeyUp="EmailSuggestBox_KeyUp"
                KeyDown="EmailSuggestBox_KeyDown" />

Methods (Note: vm.EmailOptions is just a list of email domain suggestions)

    private void EmailSuggestBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
    {
        try
        {
            if (args.Reason == AutoSuggestionBoxTextChangeReason.UserInput)
            {
                if (sender.Text.Contains('@'))
                {
                    var vm = this.DataContext as ProspectInformationEntryViewModel;
                    var d = sender.Text.Split('@');
                    var domain = d.LastOrDefault();
                    List<String> _emailSuggestion = vm.EmailOptions.Where(x => x.StartsWith(domain)).ToList();
                    sender.ItemsSource = _emailSuggestion;
                }
            }
        }
        catch (Exception)
        { }

    }
    private void EmailSuggestBox_QuerySubmitted(AutoSuggestBox sender, AutoSuggestBoxQuerySubmittedEventArgs args)
    {
        try
        {
            if (args.ChosenSuggestion != null)
            {
                sender.ItemsSource = null;
            }
        }
        catch (Exception)
        { }
    }

    private void EmailSuggestBox_SuggestionChosen(AutoSuggestBox sender, AutoSuggestBoxSuggestionChosenEventArgs args)
    {
        try
        {
            var domain = args.SelectedItem.ToString();
            var temp = sender.Text.Split('@');
            var identifier = temp.FirstOrDefault();
            sender.Text = identifier + "@" + domain;
            sender.ItemsSource = null;
        }
        catch (Exception)
        { }
    }
    private void EmailSuggestBox_KeyUp(object sender, KeyRoutedEventArgs e)
    {
        if (e.Key == Windows.System.VirtualKey.Down || e.Key == Windows.System.VirtualKey.Up)
        {
            e.Handled = true;
        }
    }

    private void EmailSuggestBox_KeyDown(object sender, KeyRoutedEventArgs e)
    {
        if (e.Key == Windows.System.VirtualKey.Down || e.Key == Windows.System.VirtualKey.Up)
        {
            e.Handled = true;
        }
    }
3 Answers

I would just like to ignore the arrow keys while typing in the AutoSuggestBox.

For your requirement, you could use ProcessKeyboardAccelerators event to intercept keydown or keyup press.

private void Autosbox_ProcessKeyboardAccelerators(UIElement sender, ProcessKeyboardAcceleratorEventArgs args)
{

    if (args.Key == VirtualKey.Down || args.Key == VirtualKey.Up)
    {
        args.Handled = true;
    }
}

Override the ToString() on your object will do the trick

Did you ever find your answer? I hope so, but to whoever find this:

I was able to do it by removing the SuggestionChosen event, letting the box update it's text with whatever the selection is at the moment (UpdateTextOnSelect set to "True"), and then using the PreviewKeyDown event to get that text when the user presses then Enter key.

Can't believe I missed this the whole time, but there's an event called QuerySubmitted. It fires upon clicking with the mouse or pressing Enter.

Related