Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/davidortinau/plugin.maui.keylistener
https://github.com/davidortinau/plugin.maui.keylistener
Last synced: 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/davidortinau/plugin.maui.keylistener
- Owner: davidortinau
- License: mit
- Created: 2023-09-25T01:44:43.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-06-06T15:50:48.000Z (7 months ago)
- Last Synced: 2024-10-11T22:24:33.426Z (3 months ago)
- Language: C#
- Size: 8.19 MB
- Stars: 15
- Watchers: 2
- Forks: 4
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
![](nuget.png)
# Plugin.Maui.KeyListener`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.
## Install Plugin
[![NuGet](https://img.shields.io/nuget/v/Plugin.Maui.KeyListener.svg?label=NuGet)](https://www.nuget.org/packages/Plugin.Maui.KeyListener/)
Available on [NuGet](http://www.nuget.org/packages/Plugin.Maui.KeyListener).
Install with the dotnet CLI: `dotnet add package Plugin.Maui.KeyListener`, or through the NuGet Package Manager in Visual Studio.
### Supported Platforms
| Platform | Minimum Version Supported |
|----------|---------------------------|
| iOS | 11+ |
| macOS | 10.15+ |
| Android | 5.0 (API 21) |
| Windows | 11 and 10 version 1809+ |## API Usage
`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(Feature.Default);`
### Regisgtration
Before you can start using `KeyboardBehavior`, you need to register to use it in your `MauiProgram`:
```csharp
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp()
.UseKeyListener();return builder.Build();
}
```### Listening
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.
```csharp
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
}
}
```# Sample
There is a sample project here in the repository. I've also built a little game [GuessWord](https://www.github.com/davidortinau/GuessWord) that uses this plugin.
# Acknowledgements
This is almost all code from @pureween, with some additions by @davidortinau and contributors! <3