Plugin.Maui.KeyListener
provides KeyboardBehavior
to listen for keyboard up (pressed) and down (released) events on any view in a .NET MAUI application. This is useful in situations where a KeyboardAccelerator
isn't suitable.
Current status: I've got macOS working, and Windows via global window events. Android and iOS need some love.
Available on NuGet.
Install with the dotnet CLI: dotnet add package Plugin.Maui.KeyListener
, or through the NuGet Package Manager in Visual Studio.
Platform | Minimum Version Supported |
---|---|
iOS | 11+ |
macOS | 10.15+ |
Android | 5.0 (API 21) |
Windows | 11 and 10 version 1809+ |
Plugin.Maui.KeyListener
provides the Feature
class that has a single property Property
that you can get or set.
You can either use it as a static class, e.g.: Feature.Default.Property = 1
or with dependency injection: builder.Services.AddSingleton<IFeature>(Feature.Default);
Before you can start using KeyboardBehavior
, you need to register to use it in your MauiProgram
:
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.UseKeyListener();
return builder.Build();
}
Add the behavior to the visual element where you want to listen for keyboard events. These events will bubble up no matter where you have focus.
public class MainPage : ContentPage
{
KeyboardBehavior keyboardBehavior = new ();
protected override void OnNavigatedTo(NavigatedToEventArgs args)
{
keyboardBehavior.KeyDown += OnKeyDown;
keyboardBehavior.KeyUp += OnKeyUp;
this.Behaviors.Add(keyboardBehavior);
base.OnNavigatedTo(args);
}
protected override void OnNavigatedFrom(NavigatedFromEventArgs args)
{
keyboardBehavior.KeyDown -= OnKeyDown;
keyboardBehavior.KeyUp -= OnKeyUp;
this.Behaviors.Remove(keyboardBehavior);
base.OnNavigatedFrom(args);
}
void OnKeyUp(object sender, KeyPressedEventArgs args)
{
// do something
}
void OnKeyDown(object sender, KeyPressedEventArgs args)
{
// do something
}
}
There is a sample project here in the repository. I've also built a little game GuessWord that uses this plugin.
This is almost all code from @pureween, with some additions by @davidortinau and contributors! <3