0

I am coding an app in window and mac platform.I have finished code like this:

<Button x:Name="ForwardButton" Text="&gt;" Command="{Binding ForwardCommand}">

and it works well.But The button is an clcik event.The user will click the button many times.So I want to change the way to an keyboard event.The right button in keyboard will be the better way to use the function. Now I do not know how to let maui with CommunityToolkit catch the keyboard event. someone show me the code like this:

<ContentPage xmlns:mvvm="clr-namespace:CommunityToolkit.Mvvm.ComponentModel;assembly=CommunityToolkit.Mvvm" ...>
    ...
    <ContentPage.Behaviors>
        <mvvm:EventToCommandBehavior EventName="KeyUp" SourceObject="{x:Reference Name=myTextBox}" Command="{Binding EnterKeyPressedCommand}"/>
    </ContentPage.Behaviors>
</ContentPage>

but unfortunately,it does not work. So Could anyone tell me what to do? I have been blocked by the problem for a long time. thank you.

2 Answers 2

1

I have found a perfect solution from msdn document. Now you can code like this:

First, create a label and use this code:

            <Label x:Name="MenuLabel" StyleClass="LabelStyle1" Text="RightClickMenu">
                <FlyoutBase.ContextFlyout>
                    <MenuFlyout>
                        <MenuFlyoutItem Text="Back" Command="{Binding BackCommand}" >
                            <MenuFlyoutItem.KeyboardAccelerators>
                                <KeyboardAccelerator Modifiers="None" Key="Left" />
                            </MenuFlyoutItem.KeyboardAccelerators>
                        </MenuFlyoutItem>
                        <MenuFlyoutItem Text="Forward" Command="{Binding ForwardCommand}" >
                            <MenuFlyoutItem.KeyboardAccelerators>
                                <KeyboardAccelerator Modifiers="None" Key="Right" />
                            </MenuFlyoutItem.KeyboardAccelerators>
                        </MenuFlyoutItem>
                        <MenuFlyoutItem Text="GoToStart" Command="{Binding GoToStartCommand}" >
                            <MenuFlyoutItem.KeyboardAccelerators>
                                <KeyboardAccelerator Modifiers="None" Key="Up" />
                            </MenuFlyoutItem.KeyboardAccelerators>
                        </MenuFlyoutItem>
                        <MenuFlyoutItem Text="GoToEnd" Command="{Binding GoToEndCommand}" >
                            <MenuFlyoutItem.KeyboardAccelerators>
                                <KeyboardAccelerator Modifiers="None" Key="Down" />
                            </MenuFlyoutItem.KeyboardAccelerators>
                        </MenuFlyoutItem>
                    </MenuFlyout>
                </FlyoutBase.ContextFlyout>
            </Label>

Second, hide the label. Now , You have keyboard event. It is so simple.

Attention!! the method is only available in .net core 8.

Good luck.

if you want to know more, you would read

https://learn.microsoft.com/en-us/dotnet/maui/user-interface/keyboard-accelerators?view=net-maui-8.0

thank you!

Sign up to request clarification or add additional context in comments.

1 Comment

NOTE: Only supported on Windows and Mac. I mention, in case you later wish to target ios or android. (Those platforms treat keys differently, because devices often don't have a hardware keyboard.)
0

There is no api can detect keyup event in the .net maui. So you can use the platform native api to do that.

  • For the Windows

Put the following code in the ContenPage.cs:

        protected override void OnHandlerChanged()
        {
            base.OnHandlerChanged();
#if WINDOWS
            var winview = this.Handler.PlatformView as Microsoft.Maui.Platform.ContentPanel;
            winview.PreviewKeyUp += (s, e) =>
            {
                if(e.Key == Windows.System.VirtualKey.Enter)
                {
                    (((BindingContext as TargetViewModel).EnterCommand) as RelayCommand).Execute(null);
                    //excute the command
                }
            };
#endif
        }
  • For the MacCatalyst

Put the following code in the \Platforms\MacCatalyst\AppDelegate.cs

public override void PressesBegan(NSSet<UIPress> presses, UIPressesEvent evt)
 {
     if(App.Current.MainPage == TargetPage)
     {
         foreach (UIPress p in presses)
         {
             var key = p.Key;
             if (key.Characters == "\r")
             {
                 ((((App.Current.MainPage as TargetPage).BindingContext as TargetViewModel).EnterCommand) as RelayCommand).Execute(null);
             }
         }
     }
     
     base.PressesBegan(presses, evt);
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.