Jim
Jim

Reputation: 43

How do I capture a KeyUp event on Windows in MAUI

In Xamarin Forms, I was able to do this via the following code in the App.xaml.cs file in the UWP project.

Window.Current.CoreWindow.KeyUp += CoreWindow_KeyUp;

I understand that because MAUI is Windows Desktop, not UWP, Window.Current is always null. So how do I capture the KeyUp event now?

Thanks for your help.

Upvotes: 2

Views: 339

Answers (1)

Jim
Jim

Reputation: 43

Figured it out. Use a Page Handler in the MauiProgram class

Add to CreateMauiApp method:

Microsoft.Maui.Handlers.PageHandler.Mapper.AppendToMapping("MyPageCustomization", (handler, view) =>
            {
    #if WINDOWS
                //Need to use PreviewKeyUp instead of KeyUp to capture the Enter Key
                handler.PlatformView.PreviewKeyUp += Windows_PreviewKeyUp;
    #endif
            });

Add method:

#if WINDOWS
private static void Windows_PreviewKeyUp(object sender, Microsoft.UI.Xaml.Input.KeyRoutedEventArgs e)    
         {
            //Code
         }
#endif

Upvotes: 2

Related Questions