How To Detect When Any Key Pressed in .NET MAUI (KeyDown Event Handler)
Asked Answered
B

2

6

I am not sure if its possible in .NET MAUI. Is anyone found solution to detect key press (including Special keys )?

Blotch answered 5/8, 2022 at 13:44 Comment(2)
Did you google the title of your question? Can see previous similar questions on SO with this query. UPDATE I just noticed you mention Special keys, so the existing answers may not apply. Maybe https://mcmap.net/q/1778872/-how-to-detect-keyboard-inputs, in your Windows folder.Lashonlashond
I'm having the same issue. I need to detect when the P1 or F1 key is pressed on a Zebra Scan Gun. The only event that comes close is the "TextChanged" event but that doesn't fire when a non-printable character key is pressed. I'm looking into the Zebra StageNow app to remap a key to send an intent. Maybe that's not the right way to go. Perhaps remap the key to replace P1 with a "+" key?Karolekarolina
T
4

This is a feature that is not available out of the box in Maui, but is feasable for Windows and Android.

For android you can subclass this.

For Windows you can hook into the native keyboard system using this library.

Trinl answered 25/9, 2022 at 23:37 Comment(4)
Is there any equivalent for Mac?Lessor
Nevermind, actually Sharphook is multiplatform!Lessor
Do you have any example as per how to configure Sharphook for MAUI or ASP core apps?Lessor
@Lessor I was able to get it work using github.com/TolikPylypchuk/SharpHook/discussions/22Blotch
D
1

Keyboard accelerators are in MAUI .NET 8. A KeyboardAccelerator can be attached to a MenuFlyoutItem by adding to its KeyboardAccelerators collection.

This can be added to ContentPage.MenuBarItems as follows

<!-- MainPage.xaml -->
<ContentPage>
    <ContentPage.MenuBarItems>
        <MenuBarItem Text="File">
            <MenuFlyoutItem Text="Down" Clicked="MenuFlyoutItem_Clicked">
                <MenuFlyoutItem.KeyboardAccelerators>
                    <KeyboardAccelerator Key="Down" />
                </MenuFlyoutItem.KeyboardAccelerators>
            </MenuFlyoutItem>
            <MenuFlyoutItem Text="Up" Clicked="MenuFlyoutItem_Clicked">
                <MenuFlyoutItem.KeyboardAccelerators>
                    <KeyboardAccelerator Key="Up" />
                </MenuFlyoutItem.KeyboardAccelerators>
            </MenuFlyoutItem>
            <MenuFlyoutItem Text="Left" Clicked="MenuFlyoutItem_Clicked">
                <MenuFlyoutItem.KeyboardAccelerators>
                    <KeyboardAccelerator Key="Left" />
                </MenuFlyoutItem.KeyboardAccelerators>
            </MenuFlyoutItem>
            <MenuFlyoutItem Text="Right" Clicked="MenuFlyoutItem_Clicked">
                <MenuFlyoutItem.KeyboardAccelerators>
                    <KeyboardAccelerator Key="Right" />
                </MenuFlyoutItem.KeyboardAccelerators>
            </MenuFlyoutItem>
        </MenuBarItem>
    </ContentPage.MenuBarItems>
</ContentPage>
// MainPage.xaml.cs
public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
    }

    private void MenuFlyoutItem_Clicked(object sender, EventArgs e)
    {
        Debug.WriteLine($"MenuFlyoutItem {((MenuFlyoutItem)sender).Text} clicked");
    }
}

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

Dissertate answered 21/11, 2023 at 5:19 Comment(1)
cool solution, is there anyway to hide the menus?Classless

© 2022 - 2024 — McMap. All rights reserved.