diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/CommandBar.xaml.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/CommandBar.xaml.cs index f4a0dc3d43..5666381d82 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/CommandBar.xaml.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/CommandBar.xaml.cs @@ -134,6 +134,15 @@ public sealed partial class CommandBar : UserControl, WeakReferenceMessenger.Default.Send(new OpenContextMenuMessage(null, null, null, ContextMenuFilterLocation.Bottom)); } + /// + /// Sets focus to the "More" button after closing the context menu, + /// keeping keyboard navigation intuitive. + /// + public void FocusMoreCommandsButton() + { + MoreCommandsButton?.Focus(FocusState.Programmatic); + } + private void ContextMenuFlyout_Opened(object sender, object e) { // We need to wait until our flyout is opened to try and toss focus diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/ContextMenu.xaml b/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/ContextMenu.xaml index e23fb815b0..5636de9aca 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/ContextMenu.xaml +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/ContextMenu.xaml @@ -15,6 +15,7 @@ xmlns:ui="using:CommunityToolkit.WinUI" xmlns:viewModels="using:Microsoft.CmdPal.UI.ViewModels" Background="Transparent" + PreviewKeyDown="UserControl_PreviewKeyDown" mc:Ignorable="d"> diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/ContextMenu.xaml.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/ContextMenu.xaml.cs index f2c2ad2f4c..afc2d190ef 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/ContextMenu.xaml.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/ContextMenu.xaml.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using CommunityToolkit.Mvvm.Messaging; +using CommunityToolkit.WinUI; using Microsoft.CmdPal.Core.ViewModels; using Microsoft.CmdPal.Core.ViewModels.Messages; using Microsoft.CmdPal.UI.Messages; @@ -115,6 +116,24 @@ public sealed partial class ContextMenu : UserControl, } } + /// + /// Handles Escape to close the context menu and return focus to the "More" button. + /// + private void UserControl_PreviewKeyDown(object sender, KeyRoutedEventArgs e) + { + if (e.Key == VirtualKey.Escape) + { + // Close the context menu (if not already handled) + WeakReferenceMessenger.Default.Send(new CloseContextMenuMessage()); + + // Find the parent CommandBar and set focus to MoreCommandsButton + var parent = this.FindParent(); + parent?.FocusMoreCommandsButton(); + + e.Handled = true; + } + } + private void ViewModel_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e) { var prop = e.PropertyName;