-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[Android] Improve Keyboard Accessibility: Support Spacebar for Android Gesture recognisers #29649
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
Hey there @@kubaflo! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR improves keyboard accessibility on Android by enabling the spacebar key to trigger gesture recognizers, aligning Android’s behavior with iOS and meeting WCAG standards.
- Introduces a new KeyPress handler to detect and handle spacebar key events.
- Adjusts focusable properties and event subscriptions to support key events alongside touch events.
Comments suppressed due to low confidence (1)
src/Controls/src/Core/Platform/GestureManager/GesturePlatformManager.Android.cs:218
- [nitpick] Consider clarifying the intention behind resetting the Focusable property to _focusableDefaultValue before conditionally setting it to true when adding touch and key event handlers to avoid potential confusion.
platformView.KeyPress -= OnKeyPress; platformView.Focusable = _focusableDefaultValue;
src/Controls/src/Core/Platform/GestureManager/GesturePlatformManager.Android.cs
Outdated
Show resolved
Hide resolved
|
I use the following code for testing: <VerticalStackLayout>
<Label Text="Tap gesture recognizer with event">
<Label.GestureRecognizers>
<TapGestureRecognizer
Tapped="TapGestureRecognizer_Tapped"/>
</Label.GestureRecognizers>
</Label>
<Label Text="Tap gesture recognizer with command">
<Label.GestureRecognizers>
<TapGestureRecognizer
Command="{Binding TapCommand}"/>
</Label.GestureRecognizers>
</Label>
<Label Text="No tap gesture recognizer"/>
<Switch>
<Switch.GestureRecognizers>
<TapGestureRecognizer
Tapped="TapGestureRecognizer_Tapped"/>
</Switch.GestureRecognizers>
</Switch>
</VerticalStackLayout>public partial class MainPage : ContentPage
{
public Command TapCommand { get; set; }
public MainPage()
{
InitializeComponent();
TapCommand = new Command(() => DisplayAlert("Success", "Command", "OK"));
BindingContext = this;
}
private void TapGestureRecognizer_Tapped(object sender, TappedEventArgs e)
{
DisplayAlert("Success", "You tapped", "OK");
}
} |
src/Controls/src/Core/Platform/GestureManager/GesturePlatformManager.Android.cs
Outdated
Show resolved
Hide resolved
src/Controls/src/Core/Platform/GestureManager/GesturePlatformManager.Android.cs
Outdated
Show resolved
Hide resolved
src/Controls/src/Core/Platform/GestureManager/GesturePlatformManager.Android.cs
Outdated
Show resolved
Hide resolved
3a166be to
225ceed
Compare
|
/azp run |
|
Azure Pipelines successfully started running 3 pipeline(s). |
src/Controls/src/Core/Platform/GestureManager/GesturePlatformManager.Android.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with @bhavanesh2001 on this one
https://github.com/dotnet/maui/pull/29649/files#r2106941403
What I wonder for this one is if there's a way to wire this in through the gesture detectors or somehow by overrides on the view
Here's the source code from android which would be ideal to mimic
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (KeyEvent.isConfirmKey(keyCode) && event.hasNoModifiers()) {
if ((mViewFlags & ENABLED_MASK) == DISABLED) {
return true;
}
if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
setPressed(false);
if (!mHasPerformedLongPress) {
// This is a tap, so remove the longpress check
removeLongPressCallback();
if (!event.isCanceled()) {
return performClickInternal();
}
}
}
}
return false;
}
27ddec1 to
91c8421
Compare
|
/azp run |
|
Azure Pipelines successfully started running 3 pipeline(s). |
| { | ||
| _innerKeyListener = new InnerKeyListener(tapRecognizers, View); | ||
| } | ||
| platformView.SetOnKeyListener(_innerKeyListener); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instead of keylistener here I think we just want to subscribe to the Key events we want
If we set the listener that's going to break anyone that's trying to subscribe to these events
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure
|
/azp run |
|
Azure Pipelines successfully started running 3 pipeline(s). |
| { | ||
| platformView.KeyPress += OnKeyPress; | ||
| } | ||
| platformView.Focusable = true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's move this up into the if block for now so we're not changing too much behavior, we only really want focusable to get set for now if they've indicated a single tap
we can review other scenarios later
|
/azp run |
|
Azure Pipelines successfully started running 3 pipeline(s). |
|
/azp run |
|
Azure Pipelines successfully started running 3 pipeline(s). |
|
@kubaflo I tried this build on Android. What works well:
However, I had the following observations as well:
For (1), I also found that making labels focusable or clickable (or both) doesn't help. but adding a click listener (Even if it doesn't do anything) to the TextView does enable highlighting. For example: Microsoft.Maui.Handlers.LabelHandler.Mapper.AppendToMapping("KeyboardFocusable", (handler, view) => For (2), I created a sample demonstrating the kind of layout I'm referring to and the observed behavior: https://github.com/sum0999/MAUIWithKeyboardSample |
@sum0999 can you include a screen shot what you mean by "not highlighted"? I'm testing on an API 28 and API 36 device and they both seem to highlight with the tap gestures
|
|
@PureWeen Added a couple of videos in addition to the screenshots to make it more clear. When hovering over a label, I can interact with it using Space/Enter, which brings up the alert, but the label is not actually highlighted at all.
If I instead use a ClickHandler like below, then I am able to get the label highlighted, and also the navigation now reaches inside of each grid's text field.
RecordingWithoutTextViewClickHandler.mp4RecordingWithTextViewClickHandler.mp4 |
|
@sum0999 Everything seems to be working as expected. Here’s a video showing the changes from this PR: the focus correctly switches to the grid and then to the label when TapGestureRecognizer is attached to these controls. After removing TapGestureRecognizer from the label, it no longer receives focus, as expected. Screen.Recording.2025-08-09.at.12.49.34.mov |
|
/azp run |
|
Azure Pipelines successfully started running 3 pipeline(s). |
…d Gesture recognizers (dotnet#29649) * Improve Keyboard Accessibility: Support Spacebar for Android Gesture Recognizers * Update GesturePlatformManager.Android.cs * - update code a bit * Update ViewExtensions.cs --------- Co-authored-by: Shane Neuville <shneuvil@microsoft.com>
…d Gesture recognizers (#29649) * Improve Keyboard Accessibility: Support Spacebar for Android Gesture Recognizers * Update GesturePlatformManager.Android.cs * - update code a bit * Update ViewExtensions.cs --------- Co-authored-by: Shane Neuville <shneuvil@microsoft.com>



…Recognizers
Note
Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!
Description of Change
Currently, .NET MAUI does not support spacebar key events for gesture recognizers on Android. This lack of support presents a significant accessibility issue, as it prevents keyboard users from activating gesture-based UI elements using the spacebar—something expected under WCAG
This discrepancy also results in inconsistent behavior between platforms, as iOS correctly responds to the spacebar key for gesture recognition.
Issues Fixed
Fixes #29648
Fixes #30685