private void lviResults_PreviewKeyDown()

in src/AccessibilityInsights.SharedUx/Controls/TestTabs/AutomatedChecksControl.xaml.cs [536:609]


        private void lviResults_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            UIElement uie = e.OriginalSource as UIElement;

            if ((e.Key == Key.Right || e.Key == Key.Left) && Keyboard.FocusedElement is ListViewItem)
            {
                if (e.Key == Key.Right)
                {
                    var vb = GetFirstChildElement<CheckBox>(sender as DependencyObject) as CheckBox;
                    vb.Focus();
                }
                else
                {
                    var parent = GetParentElem<Expander>(sender as DependencyObject) as Expander;
                    var vb = GetFirstChildElement<Label>(parent as DependencyObject) as Label;
                    vb.Focus();
                }
                e.Handled = true;
            }
            else if ((e.Key == Key.Right || e.Key == Key.Left) && (Keyboard.FocusedElement is CheckBox || Keyboard.FocusedElement is Button))
            {
                var elements = new List<DependencyObject>();
                elements.Add(GetFirstChildElement<CheckBox>(sender as DependencyObject));
                elements.AddRange(FindChildren<Button>(sender as DependencyObject));
                int selectedElementIndex = elements.FindIndex(b => b.Equals(Keyboard.FocusedElement));

                if (e.Key == Key.Right)
                {
                    if (selectedElementIndex + 1 < elements.Count)
                    {
                        ((UIElement) elements.ElementAt(selectedElementIndex + 1)).Focus();
                    }
                }
                else if (selectedElementIndex - 1 >= 0)
                {
                    ((UIElement)elements.ElementAt(selectedElementIndex - 1)).Focus();
                }
                else
                {
                    (sender as ListBoxItem).Focus();
                }
                System.Diagnostics.Debug.WriteLine(Keyboard.FocusedElement.ToString() + " " + FrameworkElementAutomationPeer.FromElement(Keyboard.FocusedElement as FrameworkElement)?.HasKeyboardFocus());
                FrameworkElementAutomationPeer.FromElement(Keyboard.FocusedElement as FrameworkElement)?.RaiseAutomationEvent(AutomationEvents.AutomationFocusChanged);
                e.Handled = true;
            }
            else if (e.Key == Key.Down || e.Key == Key.Up)
            {
                (sender as ListViewItem).Focus();
                uie = (UIElement)Keyboard.FocusedElement;

                if (e.Key == Key.Down)
                {
                    uie.MoveFocus(new TraversalRequest(FocusNavigationDirection.Down));
                }
                else
                {
                    var element = uie.PredictFocus(FocusNavigationDirection.Up);
                    if (element is ListViewItem)
                    {
                        uie.MoveFocus(new TraversalRequest(FocusNavigationDirection.Up));
                    }
                    else
                    {
                        (GetParentElem<GroupItem>(sender as DependencyObject) as UIElement).Focus();
                    }
                }
                e.Handled = true;
            }
            else if (e.Key == Key.Return && Keyboard.FocusedElement is ListViewItem)
            {
                var btn = GetFirstChildElement<Button>(sender as DependencyObject) as Button;
                ButtonElem_Click(btn, e);
            }
        }