Hooking keyboard events in .NET MAUI (specifically Android)

Viewed 811

I'm porting an Android app that I prototyped in Xamarin Forms. The device that I'm targeting has hardware buttons that must trigger actions in the application. In Xamarin I was able to override ICallback.OnKeyDown(Keycode keyCode,KeyEvent e) in an Activity. I haven't been able to find any equivalent in MAUI. I was expecting <InputBinding/> like WPF or maybe an event or override on an Element. Is there any way I can achieve this? Maybe even with Android-specific code?

1 Answers

Android doesn't do keyboard events from the soft keyboard. It does commitText calls. Some frameworks fake keyboard events from it, but they're not accurate. The onKeyDown in Activity is only applicable to physical keys- power, volume, etc. So you can't rely on keyboard events happening at all, and even when they do, you can't trust any data in them other than printable keys. Timing data, up and downs, and meta keys (shift, ctrl, etc) will be completely wrong.

The Android way to do things is to use a TextWatcher on the text field. I have no idea how that translates to your framework.

Related