Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Key presses and key combinations on inputs and pages #3858

@bwoebi

Description

@bwoebi

Description

On Desktop, and also on mobile with hardware keyboards, one may press key combinations. It should be possible to intercept these key presses with MAUI, on the page and input (basically focussable elements - mainly Entry and Editor) level.

The event is first delivered to focussed elements, then to the page as last resort.

On an Editor, the events would be equivalent to e.g. TextBox.PreviewKeyDown on uwp or AppCompatEditText.KeyPress (with Android.Views.View.KeyEventArgs.Event?.Action == Android.Views.KeyEventActions.Down) on Android and UIResponder.PressesBegan on iOS/MacCatalyst. Similarly KeyUp can be implemented. For virtual keyboards on iOS, the KeyUp and KeyDown events can be emulated with support from e.g. UITextFieldDelegate.ShouldChangeCharacters, to provide a seamless experience.

Edit: I just found #3739 - which is quite related, but what I'm suggesting here is listening to specific key events in a streamlined way. That issue is only global events and exposing Keyboard State access, which is also desirable, additionally to this feature.

Public API Changes

Usage example

// Similarly possible with KeyUp
MyEditor.KeyDown += (object sender, KeyEventArgs e) {
  if (e.Key == Key.Enter && e.Shift) {
    SubmitText(MyEditor.Text);
    MyEditor.Text = "";
    e.Handled = true;
  }
}; 

Public API suggestion

class KeyEventArgs {
  public bool Handled = false;
  public KeyCode Key { get; }
  public string? Characters { get; } // like Android.Views.KeyEvent.characters or the ToAscii function from user32.dll 
  public bool Shift { get; }
  public bool Ctrl { get; }
  public bool Alt { get; }
  public bool Meta { get; }
  public bool HasModifiers => Shift || Ctrl || Alt || Meta;
  public bool VirtualKeyboard { get; } // to distinguish whether the key was pressed on hardware or on-screen keyboard
}

// this enum contains all KeyCodes possibly present on any OS. There must be a proper mapping for each target.
enum KeyCode {
  A,
  B,
  Enter,
  // etc - with some platform independent numbering
}

I'm trying to keep the API minimal for this issue, but there may be more common aspects on all targets which can be included here.

Intended Use-Case

A trivial shortcut like the ctrl+s key combination could trigger a save.

Pressing a key like shift+enter over a specific selection in an editor could trigger a submission (instead of inserting a new line).

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-controls-entryEntrypartnerIssue or Request from a partner teampartner/cat 😻this is an issue that impacts one of our partners or a customer our advisory team is engaged withproposal/open

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions