Skip to content
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

fix: Button should not fire the click event on the space key when it is not active #16619

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/Avalonia.Controls/Button.cs
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,9 @@ protected override void OnKeyDown(KeyEventArgs e)
OnClick();
e.Handled = true;
break;

case Key.Space:
// Handle Space only current focus control
MrJul marked this conversation as resolved.
Show resolved Hide resolved
if (TopLevel.GetTopLevel(this)?.FocusManager?.GetFocusedElement() == this)
MrJul marked this conversation as resolved.
Show resolved Hide resolved
{
if (ClickMode == ClickMode.Press)
{
Expand All @@ -299,22 +300,21 @@ protected override void OnKeyDown(KeyEventArgs e)

IsPressed = true;
e.Handled = true;
break;
}

break;
case Key.Escape when Flyout != null:
// If Flyout doesn't have focusable content, close the flyout here
CloseFlyout();
break;
}

base.OnKeyDown(e);
}

/// <inheritdoc/>
protected override void OnKeyUp(KeyEventArgs e)
{
if (e.Key == Key.Space)
// Handle Space only current focus control
MrJul marked this conversation as resolved.
Show resolved Hide resolved
if (e.Key == Key.Space && TopLevel.GetTopLevel(this)?.FocusManager?.GetFocusedElement() == this)
MrJul marked this conversation as resolved.
Show resolved Hide resolved
{
if (ClickMode == ClickMode.Release)
{
Expand Down
28 changes: 28 additions & 0 deletions tests/Avalonia.Controls.UnitTests/ButtonTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -511,11 +511,39 @@ public void Button_CommandParameter_Does_Not_Change_While_Execution()
(target as IClickableControl).RaiseClick();
}

[Fact]
void Should_Not_Fire_Click_Event_On_Space_Key_When_It_Is_Not_Focus()
{
using (UnitTestApplication.Start(TestServices.StyledWindow))
{
var raised = 0;
var target = new TextBox();
var button = new Button()
{
Content = target,
};

var window = new Window { Content = button };
window.Show();

button.Click += (s, e) => ++raised;
target.Focus();
target.RaiseEvent(CreateKeyDownEvent(Key.Space));
target.RaiseEvent(CreateKeyUpEvent(Key.Space));
Assert.Equal(0, raised);
}
}

private KeyEventArgs CreateKeyDownEvent(Key key, Interactive source = null)
{
return new KeyEventArgs { RoutedEvent = InputElement.KeyDownEvent, Key = key, Source = source };
}

private KeyEventArgs CreateKeyUpEvent(Key key, Interactive source = null)
{
return new KeyEventArgs { RoutedEvent = InputElement.KeyUpEvent, Key = key, Source = source };
}

private void RaisePointerPressed(Button button, int clickCount, MouseButton mouseButton, Point position)
{
_helper.Down(button, mouseButton, position, clickCount: clickCount);
Expand Down
Loading