Key presses and key combinations on inputs and pages
See original GitHub issueDescription
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).
Issue Analytics
- State:
- Created 2 years ago
- Reactions:33
- Comments:9 (3 by maintainers)

Top Related StackOverflow Question
How in the world, a
UI frameworkgot out of the gate with no mechanisms for responding to keyboard events baffles me, to put it mildly.Duplicate of #16202