4

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;
        }
    }
hendch
  • 41
  • 3
  • The arrow keys are part of the default behavior one woukd expect. You should only disable them if you know that your users won't expect them to work as usual – NielsNet Sep 11 '18 at 19:24
  • 1
    try using PreviewKeyDown and PreviewKeyUpInstead, these 2 fire before KeyUp and KeyDown Respectively. – Muhammad Touseef Sep 11 '18 at 19:26
  • @NielsNet Just edited post. The up/down arrow key forces the user to select the last/first item respectively. I don't need to disable them, but I would like for the user to be able to choose from more than 2 selections in the suggestion list. – hendch Sep 11 '18 at 19:45
  • @touseefbsb Unfortunately there are no PreviewKeyDown and PreviewKeyUp events for AutoSuggestBox – hendch Sep 11 '18 at 19:46
  • I guess it is only available for sdk creators update and above. – Muhammad Touseef Sep 11 '18 at 19:51
  • The thing is that the essentially highlighting a suggestion modifies the text to match the suggestion so the event is equal to "suggestion chosen" – Martin Zikmund Sep 11 '18 at 22:21

2 Answers2

3

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;
    }
}
Nico Zhu
  • 32,367
  • 2
  • 15
  • 36
  • by doing this up/down completely stop working and we cant enter the suggestion list at all with arrow keys, can we enter the list with arrow keys without firing suggestion chosen ? – Muhammad Touseef May 04 '20 at 11:05
0

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

kaycee
  • 1,199
  • 4
  • 24
  • 42