CommunityToolkit / Maui

The .NET MAUI Community Toolkit is a community-created library that contains .NET MAUI Extensions, Advanced UI/UX Controls, and Behaviors to help make your life as a .NET MAUI developer easier
https://learn.microsoft.com/dotnet/communitytoolkit/maui
MIT License
2.29k stars 404 forks source link

Proposal: Implementation of Native Keyboard Events in .NET MAUI #1948

Closed mateusfrancino closed 5 months ago

mateusfrancino commented 5 months ago

Feature name

Shortcut Events

Link to discussion

1947

Progress tracker

Android Implementation iOS Implementation MacCatalyst Implementation Windows Implementation Tizen Implementation Unit Tests Samples Documentation Summary

Currently, .NET MAUI does not natively support keyboard events such as KeyDown and KeyUp, which were widely used in Windows Forms. This limitation significantly impacts the development of applications that rely on keyboard-based interactions, such as point-of-sale (POS) systems, where keyboard shortcuts are essential for operational efficiency.

Motivation

Why are we doing this?

Developing robust interactive functionality in modern applications, especially those aimed at the commercial environment such as POS systems, often depends on fast responses to input commands. However, the lack of broad support for keyboard events in .NET MAUI imposes substantial limitations on developers and degrades the user experience, forcing the adoption of workarounds such as the use of third-party libraries. These workarounds can lead to inconsistencies, increased development time, and an unnecessarily steep learning curve.

What use cases does it enable?

Implementing native keyboard events allows for a variety of use cases, such as:

Point of Sale (POS) Systems: Where operators can benefit from keyboard shortcuts for quick data entry and navigation, increasing operational efficiency. Editing and Content Creation Applications: Allowing shortcuts for commands such as saving, loading, and editing, making the user experience more fluid and intuitive. Games and Simulations: Where an immediate response to keyboard commands can be crucial to gameplay.

What is the expected result?

The expected result of implementing this functionality is a significant improvement in developers' ability to create interactive and efficient user interfaces. This not only improves the end user experience by making applications more intuitive and easier to use, it also aligns .NET MAUI more closely with the functionality available on more traditional, established development platforms such as Windows Forms. This improvement would help to consolidate .NET MAUI as a high-performance and versatile development tool, suitable for a wider range of commercial and entertainment applications.

Detailed Design

Functionality Overview

The proposal consists of integrating a keyboard event model directly into the .NET MAUI core, allowing developers to register and respond to key events in any part of the application, in a consistent and efficient way. This model covers not only simple key events but also key combinations.

Components Involved

MAUI Core: Implementation of basic keyboard events. Community Toolkit: Extensions to simplify and expand the use of keyboard events, facilitating common implementation of patterns and best practices.

Proposed Design

Base Events: Implement three main events in the lifecycle of a key:

KeyDown : Triggered when a key is pressed. KeyUp : Triggered when a key is released. KeyPress : Fired when a key results in a specific character. Event Classes: KeyEventArgs would be the base class, containing information about the key pressed, including:

Key: The specific key pressed. Modifiers: Key combinations like Shift, Ctrl, Alt. Integration with the Community Toolkit

To ensure that the community can easily extend the use of keyboard events, the Community Toolkit could include helpers and behaviors that simplify registering and responding to keyboard events, especially for MVVM commands and other declarative interactions.

Usage Syntax

public MainPage() { InitializeComponent(); this.KeyDown += HandleKeyDown; }

private void HandleKeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Keys.F1 && e.Modifiers == KeyModifiers.None)
    {
        DisplayHelp();
    }
    else if (e.Key == Keys.C && e.Modifiers == KeyModifiers.Control)
    {
        CopySelection();
    }
}

private void DisplayHelp()
{
    // Mostra ajuda
}

private void CopyCopySelection()
{
    // Copia seleção
}

Drawbacks

No response

Alternatives

No response

Unresolved Questions

No response

vhugogarcia commented 5 months ago

Thanks for the proposal @mateusfrancino, since this will require further discussion from the community, I'm moving this to discussions.