question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Key presses and key combinations on inputs and pages

See original GitHub issue

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).

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:33
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

12reactions
eidyloncommented, Apr 21, 2023

How in the world, a UI framework got out of the gate with no mechanisms for responding to keyboard events baffles me, to put it mildly.

0reactions
samhoutscommented, Jul 27, 2023

Duplicate of #16202

Read more comments on GitHub >

github_iconTop Results From Across the Web

Keyboard shortcuts for Pages on Mac
In Pages on your Mac, use keyboard shortcuts to move around in documents, edit and format text, edit data in tables, move objects,...
Read more >
Capture key press without placing an input element on the ...
Detect key press, including key combinations: window.addEventListener('keydown', function (e) { if (e.ctrlKey && e.
Read more >
Keyboard Input (Get Started with Win32 and C++)
The keyboard is used for several distinct types of input, including: Character input. Text that the user types into a document or edit...
Read more >
Handle keyboard actions
Handle single key events​​ To handle an individual key press, implement onKeyDown() or onKeyUp() , as appropriate. Usually, you use onKeyUp() if ...
Read more >
KeyboardEvent - Web APIs | MDN
KeyboardEvent objects describe a user interaction with the keyboard; each event describes a single interaction between the user and a key ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found