dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.06k stars 1.73k forks source link

[WIN] MAUI Not Returning and Sending Microsoft.UI.Xaml.Input.PreviewKeyDown Event When Subscribed. #23789

Open cgp1976 opened 2 months ago

cgp1976 commented 2 months ago

Description

There are two issues here

  1. MAUI Not Returning Microsoft.UI.Xaml.Input.PreviewKeyDown Event When Subscribed.
  2. MAUI Stop Sending Microsoft.UI.Xaml.Input.PreviewKeyDown Event When Subscribed.
var platformView = this.Handler.PlatformView as Microsoft.Maui.Platform.ContentPanel;
platformView.PreviewKeyDown += PlatformView_PreviewKeyDown;

private void PlatformView_PreviewKeyDown(object sender, Microsoft.UI.Xaml.Input.KeyRoutedEventArgs e)
{
    Debug.WriteLine($"Invoked @ {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")}");
}

For Point 1 if the entire layout doesn't have a button control. this will never trigger the event.

For Point 2 We put a button control in the grid and from there onwards, we are able to receive the event but for some reasons, this will only work two times after it successfully invoked.

Steps to Reproduce

MauiApp1.zip

Unzipped the attached file

For Point 1 Go to MainPage.xaml and replace the entire Grid xaml with the following

    <Grid BackgroundColor="Gray">
        <Label BackgroundColor="Pink" Text="Label" VerticalTextAlignment="Center" HorizontalTextAlignment="Center" HeightRequest="30" WidthRequest="100"></Label>
    </Grid>

Run and Debug the project , once page loaded, you can input some keyboard input to see if event was invoked or not. Expected - PlatformView_PreviewKeyDown is never called

For Point 2 Go to MainPage.xaml and replace the entire Grid xaml with the following

    <Grid BackgroundColor="Gray">
        <Button Text="Button" BackgroundColor="Blue" HeightRequest="30" WidthRequest="100"></Button>
    </Grid>

Run and Debug the project , once page loaded, you can input some keyboard input to see if event was invoked or not. Expected - PlatformView_PreviewKeyDown was Invoked, and it will fail after two times

Link to public reproduction project repository

No response

Version with bug

9.0.0-preview.6.24327.7

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

Windows

Affected platform versions

No response

Did you find any workaround?

nil

Relevant log output

No response

github-actions[bot] commented 2 months ago

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!

Open similar issues:

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

Foda commented 1 month ago

I've verified that this also doesn't work with NET8 versions, but I don't think it's a bug. I suspect that the preview key down events only trigger when a control has focus (ie: the button).

If your goal is to invoke commands, keyboard accelerators might be a solution: https://learn.microsoft.com/en-us/dotnet/maui/user-interface/keyboard-accelerators

dotnet-policy-service[bot] commented 3 days ago

Hi @cgp1976. We have added the "s/needs-info" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.