简体   繁体   中英

MAUI How to use KeyEvent.Callback in an Android app

The application is written for the data collection terminal (android). It has a physical keyboard. Can you tell me how to intercept keyboard clicks if there is no text field on the form?

The OnKeyListener Class can be used to deal with the input from the physical keyboard:

 public class MyListener : Java.Lang.Object, Android.Views.View.IOnKeyListener
{
    public bool OnKey(Android.Views.View v, [GeneratedEnum] Keycode keyCode, KeyEvent e)
    {
         return true;// true will intercept keyboard clicks
    }
}

And add the listener to the entry when there is no text field on the form:

private void entry_Focused(object sender, FocusEventArgs e)
{
    Entry entry = (Entry)sender;
#if ANDROID
    (entry.Handler.PlatformView as AppCompatEditText).SetOnKeyListener(new MyListener());
#endif
 }

And when the form has text, remove the listener:

Entry entry = (Entry)sender;
#if ANDROID
    (entry.Handler.PlatformView as AppCompatEditText).SetOnKeyListener(null);
#endif

A solution that I have found is this:

  1. Define an interface to implement in your pages that you want to "react" to physical keyboard keys (optional - it is used to differentiate pages that you want to react to keys from pages that is not necessary to). A sample:

     #if ANDROID using Android.Views; #endif namespace KeyboardTest { public interface IOnPageKeyDown { #if ANDROID /// <summary> /// Called when a key is pressed. /// </summary> /// <param name="keyCode">The code of pressed key.</param> /// <param name="e">Description of the key event.</param> /// <returns> /// Return true to prevent this event from being propagated further, /// or false to indicate that you have not handled this event and it should continue to be propagated. /// </returns> public bool OnPageKeyDown(Keycode keyCode, KeyEvent e); #endif } }
  2. Implement this interface to every page you want to react to keys. Sample:

     #if ANDROID using Android.Views; #endif // Your code here namespace KeyboardTest { public partial class TestPage: ContentPage, IOnPageKeyDown { #if ANDROID public bool OnPageKeyDown(Keycode keyCode, KeyEvent e) { switch (keyCode) { case Keycode.DpadUp: // Your code here return true; case Keycode.DpadDown: // Your code here return true; case Keycode.Enter: // Your code here return true; default: return false; } } #endif } }
  3. Override "OnKeyDown" in Platforms/Android/MainActivity.cs. Sample:

     using Android.App; using Android.Content.PM; using Android.OS; using Android.Runtime; using Android.Views; using AndroidX.Core.View; namespace KeyboardTest; // Your code here public class MainActivity: MauiAppCompatActivity { // Your code here public override bool OnKeyDown([GeneratedEnum] Keycode keyCode, KeyEvent e) { Page p = Shell.Current.CurrentPage; if (p is IOnPageKeyDown) { bool handled = (p as IOnPageKeyDown).OnPageKeyDown(keyCode, e); if (handled) return true; else return base.OnKeyDown(keyCode, e); } else return base.OnKeyDown(keyCode, e); } }

I hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM