microsoft / microsoft/WindowsAppSDK

FileOpenPicker (et al) does not restore keyboard focus properly to the window

Open
#6,505 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

area-File access needs-triage
Dominant language
C++
Stars
4.7k
Forks
471
Avg merge
3d 13h
Merged PRs (30d)
28

Description

Describe the bug

Show a FileOpenPicker. After it closes, keyboard events do not seem to be invoked (until the user clicks somewhere) even though the window is focussed and FocusManager.GetFocusedElement reports a focussed element. The issue can be worked around by adding (FocusManager.GetFocusedElement(Content.XamlRoot) as UIElement)?.Focus(FocusState.Programmatic) after picking a file.

Steps to reproduce the bug

Use the following minimal code.

 public sealed partial class MainWindow : Window {
     public MainWindow() {
         InitializeComponent();
     }
     private void Grid_KeyDown(object sender, KeyRoutedEventArgs e) {
         System.Diagnostics.Debug.WriteLine("Key pressed");
     }
     private async void testStoragePickerButton_Click(object sender, RoutedEventArgs e) {
         var filePicker = new FileOpenPicker(AppWindow.Id);
         filePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
         filePicker.FileTypeFilter.Add("*");
         try {
             var file = await filePicker.PickSingleFileAsync();
         } catch (Exception) { }
         System.Diagnostics.Debug.WriteLine(FocusManager.GetFocusedElement(Content.XamlRoot)); //Note that there is a focussed element, but no key press events are generated
         //(FocusManager.GetFocusedElement(Content.XamlRoot) as UIElement)?.Focus(FocusState.Programmatic); //Uncomment this to restore keyboard focus
     }
 }
<?xml version="1.0" encoding="utf-8"?>
<Window x:Class="TestFilePickerFocus.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:TestFilePickerFocus" Title="TestFilePickerFocus">
    <Grid KeyDown="Grid_KeyDown">
        <Button x:Name="testStoragePickerButton" Click="testStoragePickerButton_Click" Content="Pick file"></Button>
    </Grid>
</Window>

Click the button, pick a file, then press any key (even enter, which should press the button again), and observe no "Key pressed" message in debug output.

Expected behavior

Keyboard focus should be properly returned.

Screenshots

No response

NuGet package version

2.1.3

Packaging type

Packaged (MSIX)

Windows version

Windows 11 version 24H2 LTSC (26100, June Update)

IDE

No response

Additional context

No response

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the minimal C# reproduction using FileOpenPicker, FocusManager.GetFocusedElement, and the Grid_KeyDown handler. Reproduce the focus loss after PickSingleFileAsync, then trace the FileOpenPicker close and focus-restoration path; done means keyboard events return without the caller's manual UIElement.Focus workaround.

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
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.