KeyDown Bubbling Event Not Triggered After Navigation and Focus Gain
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 9.6k
- Forks
- 1k
- PR merge metrics
- No merged PRs in 30d
Description
Describe the bug
After navigating to any interface and once the interface gains focus, I've noticed that the KeyDown event (a bubbling event) often fails to trigger. This issue primarily affects the arrow keys: Up, Down, Left, and Right. I suspect this may be due to the PreviewKeyDown event (a tunneling event) being handled prematurely, which prevents the bubbling KeyDown event from being properly triggered.
To Reproduce
Steps to Reproduce
Step 1: Add tunneling and bubbling key event subscriptions in the main window constructor:
public MainWindow(
MainWindowViewModel viewModel,
INavigationService navigationService,
IServiceProvider serviceProvider,
ISnackbarService snackbarService,
IContentDialogService contentDialogService
)
{
Appearance.SystemThemeWatcher.Watch(this);
ViewModel = viewModel;
DataContext = this;
InitializeComponent();
snackbarService.SetSnackbarPresenter(SnackbarPresenter);
navigationService.SetNavigationControl(NavigationView);
contentDialogService.SetDialogHost(RootContentDialog);
PreviewKeyDown += OnPreviewKeyDown;
KeyDown += OnKeyDown;
}
private void OnKeyDown(object sender, KeyEventArgs e)
{
Debug.WriteLine($"OnKeyDown {e.Key}");
}
private void OnPreviewKeyDown(object sender, KeyEventArgs e)
{
Debug.WriteLine($"OnPreviewKeyDown {e.Key}");
}
Step 2: Start the application and without performing any actions, test key presses. The output is as follows:
21:05:06:923 OnPreviewKeyDown Up
21:05:06:923 OnKeyDown Up
21:05:07:169 OnPreviewKeyDown Down
21:05:07:169 OnKeyDown Down
21:05:07:416 OnPreviewKeyDown Left
21:05:07:416 OnKeyDown Left
21:05:07:416 OnPreviewKeyDown Right
21:05:07:416 OnKeyDown Right
Step 3: Click any menu item in the navigation bar (e.g., Base Input) so that the focus moves to the navigation bar. Press the keys again and observe the output:
21:08:04:890 OnPreviewKeyDown Up
21:08:05:639 OnPreviewKeyDown Down
21:08:05:889 OnPreviewKeyDown Left
21:08:06:141 OnPreviewKeyDown Right
Step 4: Click on the first gallery navigation item Anchor and press the keys again. The output is as follows:
21:11:16:533 OnPreviewKeyDown Up
21:11:16:533 OnKeyDown Up
21:11:16:777 OnPreviewKeyDown Down
21:11:16:777 OnKeyDown Down
21:11:16:777 OnPreviewKeyDown Left
21:11:16:777 OnKeyDown Left
21:11:17:029 OnPreviewKeyDown Right
21:11:17:029 OnKeyDown Right
Step 5 (Critical Step): Click any element on this page to gain focus (e.g., expand the source code), then press the keys again. The output is as follows:
21:12:57:145 OnPreviewKeyDown Up
21:12:57:391 OnPreviewKeyDown Down
21:12:57:391 OnPreviewKeyDown Left
21:12:57:637 OnPreviewKeyDown Right
However, other keys can still trigger the bubbling event:
21:14:23:389 OnPreviewKeyDown Up
21:14:23:639 OnPreviewKeyDown Down
21:14:23:889 OnPreviewKeyDown Left
21:14:23:889 OnPreviewKeyDown Right
21:14:25:640 OnPreviewKeyDown A
21:14:25:640 OnKeyDown A
21:14:26:387 OnPreviewKeyDown B
21:14:26:387 OnKeyDown B
21:14:26:639 OnPreviewKeyDown C
21:14:26:639 OnKeyDown C
Expected behavior
Please avoid handling key events directly in the tunneling event, as this can prevent text editors on the page from properly handling cursor movement and can also disrupt key event propagation for the WebView2 control. This behavior may negatively impact keyboard-based user interactions and reduce overall user experience.
Moreover, when I created a new window and moved the page content to this new window, the text editor’s cursor movement function worked correctly, and the WebView2 control could receive key events. This proves that the previously described issue does not occur in the new window, confirming that the bug originates from the navigation bar.
Screenshots
No response
OS version
Edition: Windows 11 Pro
Version: 23H2
Installation Date: February 29, 2024
OS Build: 22631.4317
Experience: Windows Feature Experience Pack 1000.22700.1041.0
.NET version
.NET 8.0
WPF-UI NuGet version
Version: 3.0.5, and the testing was conducted using the source code repository cloned two days ago, with the same issue observed.
Additional context
It should not be related to <ui:NavigationView.AutoSuggestBox>, as it was removed and tested without it.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at the MainWindow constructor and inspect the PreviewKeyDown and KeyDown subscriptions around NavigationView focus handling. Reproduce the arrow-key behavior after selecting a navigation item and focusing page content, then trace the navigation bar's event handling. Done means bubbling KeyDown events still reach the window without disrupting text-editor cursor movement or WebView2 input.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- desktop
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100