protected override void OnPreviewKeyDown(KeyEventArgs e)
 {
     if (e.Handled)
     {
         return;
     }
     if (e.Key == Key.Down)
     {
         DependencyObject element = e.OriginalSource as DependencyObject;
         if (element != null)
         {
             UIElement footerPaneHost = FooterPaneHost;
             if (footerPaneHost != null &&
                 footerPaneHost.IsKeyboardFocusWithin &&
                 TreeHelper.IsVisualAncestorOf(footerPaneHost, element))
             {
                 DependencyObject nextFocus = RibbonHelper.PredictFocus(element, FocusNavigationDirection.Down);
                 if (nextFocus == null ||
                     nextFocus == element)
                 {
                     // If the focus is on the last element of footer pane,
                     // then try moving focus into items pane and then into
                     // auxiliary pane if needed.
                     if (ItemsPaneMoveFocus(FocusNavigationDirection.First) ||
                         AuxiliaryPaneMoveFocus(FocusNavigationDirection.First))
                     {
                         e.Handled = true;
                     }
                 }
             }
         }
     }
     else if (e.Key == Key.Up)
     {
         UIElement popupChild = _popup.TryGetChild();
         if (popupChild != null &&
             !popupChild.IsKeyboardFocusWithin)
         {
             // If the popup does not have focus with in then try moving focus to
             // last element of FooterPane and then to the last element of
             // auxiliary pane if needed.
             if (FooterPaneMoveFocus(FocusNavigationDirection.Last) ||
                 AuxiliaryPaneMoveFocus(FocusNavigationDirection.Last))
             {
                 e.Handled = true;
             }
         }
         else
         {
             DependencyObject element = e.OriginalSource as DependencyObject;
             if (element != null)
             {
                 UIElement auxilaryPaneHost = AuxiliaryPaneHost;
                 if (auxilaryPaneHost != null &&
                     auxilaryPaneHost.IsKeyboardFocusWithin &&
                     TreeHelper.IsVisualAncestorOf(auxilaryPaneHost, element))
                 {
                     DependencyObject nextFocus = RibbonHelper.PredictFocus(element, FocusNavigationDirection.Up);
                     if (nextFocus == null ||
                         nextFocus == element)
                     {
                         // If the focus is on last first element of auxiliary pane,
                         // then try moving focus to last element of Items Pane and
                         // then to last element of FooterPane if needed.
                         if (ItemsPaneMoveFocus(FocusNavigationDirection.Last) ||
                             FooterPaneMoveFocus(FocusNavigationDirection.Last))
                         {
                             e.Handled = true;
                         }
                     }
                 }
             }
         }
     }
     else if (e.Key == Key.Left ||
              e.Key == Key.Right)
     {
         DependencyObject element = e.OriginalSource as DependencyObject;
         if (element != null)
         {
             if ((e.Key == Key.Left) == (FlowDirection == FlowDirection.LeftToRight))
             {
                 UIElement auxilaryPaneHost = AuxiliaryPaneHost;
                 if (auxilaryPaneHost != null &&
                     auxilaryPaneHost.IsKeyboardFocusWithin &&
                     TreeHelper.IsVisualAncestorOf(auxilaryPaneHost, element))
                 {
                     // If the effective key is left and the focus is on left most element
                     // of auxiliary pane, then move focus to nearest element to the left of
                     // auxiliarypane.
                     DependencyObject nextFocus = RibbonHelper.PredictFocus(element, FocusNavigationDirection.Last);
                     if (nextFocus != null &&
                         !TreeHelper.IsVisualAncestorOf(auxilaryPaneHost, nextFocus))
                     {
                         if (RibbonHelper.Focus(nextFocus))
                         {
                             e.Handled = true;
                         }
                     }
                 }
             }
             else if (e.Key == Key.Left)
             {
                 ScrollViewer subMenuScrollViewer = SubMenuScrollViewer;
                 if (subMenuScrollViewer != null &&
                     subMenuScrollViewer.IsKeyboardFocusWithin &&
                     TreeHelper.IsVisualAncestorOf(subMenuScrollViewer, element))
                 {
                     // If the flow direction is RightToLeft and the key is Left,
                     // and the focus is in items pane, move the the focus outside teh
                     // items pane if needed.
                     RibbonMenuItem menuItem = element as RibbonMenuItem;
                     if (menuItem == null)
                     {
                         menuItem = TreeHelper.FindVisualAncestor <RibbonMenuItem>(element);
                     }
                     if (menuItem != null &&
                         !menuItem.CanOpenSubMenu)
                     {
                         DependencyObject nextFocus = menuItem.PredictFocus(FocusNavigationDirection.Right);
                         if (nextFocus != null)
                         {
                             if (RibbonHelper.Focus(nextFocus))
                             {
                                 e.Handled = true;
                             }
                         }
                     }
                 }
             }
         }
     }
     base.OnPreviewKeyDown(e);
 }