Skip to content

Commit

Permalink
Do not match access keys on input elements that are not effectively e…
Browse files Browse the repository at this point in the history
…nabled. (#13185)
  • Loading branch information
boydpatterson authored and grokys committed Oct 13, 2023
1 parent 031d7d2 commit dfe0130
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Avalonia.Base/Input/AccessKeyHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ protected virtual void OnKeyDown(object? sender, KeyEventArgs e)
var text = e.Key.ToString();
var matches = _registered
.Where(x => string.Equals(x.AccessKey, text, StringComparison.OrdinalIgnoreCase)
&& x.Element.IsEffectivelyVisible)
&& x.Element.IsEffectivelyVisible
&& x.Element.IsEffectivelyEnabled)
.Select(x => x.Element);

// If the menu is open, only match controls in the menu's visual tree.
Expand Down
24 changes: 24 additions & 0 deletions tests/Avalonia.Base.UnitTests/Input/AccessKeyHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,30 @@ public void Should_Raise_AccessKeyPressed_For_Registered_Access_Key()
Assert.Equal(1, raised);
}

[Fact]
public void Should_Not_Raise_AccessKeyPressed_For_Registered_Access_Key_When_Not_Effectively_Enabled()
{
var button = new Button();
var root = new TestRoot(button) { IsEnabled = false };
var target = new AccessKeyHandler();
var raised = 0;

target.SetOwner(root);
target.Register('A', button);
button.AddHandler(AccessKeyHandler.AccessKeyPressedEvent, (s, e) => ++raised);

KeyDown(root, Key.LeftAlt);
Assert.Equal(0, raised);

KeyDown(root, Key.A, KeyModifiers.Alt);
Assert.Equal(0, raised);

KeyUp(root, Key.A, KeyModifiers.Alt);
KeyUp(root, Key.LeftAlt);

Assert.Equal(0, raised);
}

[Fact]
public void Should_Open_MainMenu_On_Alt_KeyUp()
{
Expand Down

0 comments on commit dfe0130

Please sign in to comment.