dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
21.99k stars 1.72k forks source link

[Android, 9.0 Preview 7] Overriding OnKeyUp in MainActivity does not capture keyboard presses #24557

Open janne-hmp opened 2 weeks ago

janne-hmp commented 2 weeks ago

Description

I'm using the following code in Xamarin and .NET MAUI to capture external hardware keyboard key presses on Android in the MainActivity class:

    public override bool OnKeyUp([GeneratedEnum] Keycode keyCode, KeyEvent e)
    {
        bool wasHandled = PlatformService.HandleOnKeyUp(keyCode, e);
        if (!wasHandled)
            return base.OnKeyUp(keyCode, e);
        else
            return true;
    }

In Xamarin, the key presses are successfully captured (and then handled by my HandleOnKeyUp function), but in .NET MAUI the event never fires. Instead, the arrow keys move some sort of a selection rectangle around various UI elements in the screen. Other keys do not do anything.

Is there a way to switch of this selection navigation behavior so that the OnKeyUp would fire like in Xamarin Android? I suppose the key presses are now possibly being captured before MainActivity by one of the MAUI elements to implement the selection rectangle behavior. Or is there another way to capture the key presses in MAUI on Android?

Steps to Reproduce

  1. Build GnollHackM for Android
  2. Connect external Bluetooth keyboard to your device
  3. Start the game by pressing Play Game
  4. Keyboard commands do not do anything, except for arrow keys that move a selection rectangle around

Link to public reproduction project repository

https://github.com/hyvanmielenpelit/GnollHack

Version with bug

9.0.0-preview.7.24407.4

Is this a regression from previous behavior?

Yes, this used to work in Xamarin.Forms

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

Android 14

Did you find any workaround?

No, external keyboard does not seem to work on Android in .NET MAUI, except for moving the selection rectangle around

Relevant log output

No response

github-actions[bot] commented 2 weeks ago

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!

Open similar issues:

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

moljac commented 1 week ago

I was able to reproduce

dotnet-test.zip

Screenshot_20240905-161110 Screenshot_20240905-161552