Quantcast
Channel: User Liyun Zhang - MSFT - Stack Overflow
Viewing all articles
Browse latest Browse all 444

Answer by Liyun Zhang - MSFT for how to handle keyboard event in maui with CommunityToolkit

$
0
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); }

Viewing all articles
Browse latest Browse all 444

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>