diff --git a/Terminal.Gui/Views/Button.cs b/Terminal.Gui/Views/Button.cs
index c5995f838d..0c90c13b8e 100644
--- a/Terminal.Gui/Views/Button.cs
+++ b/Terminal.Gui/Views/Button.cs
@@ -129,6 +129,10 @@ void Initialize (ustring text, bool is_default)
/// Gets or sets whether the is the default action to activate in a dialog.
///
/// true if is default; otherwise, false.
+ ///
+ /// If is the current focused view
+ /// will remain focused if the window is not closed.
+ ///
public bool IsDefault {
get => is_default;
set {
@@ -219,7 +223,7 @@ bool ExecuteColdKey (KeyEvent ke)
bool AcceptKey ()
{
- if (!HasFocus) {
+ if (!IsDefault && !HasFocus) {
SetFocus ();
}
OnClicked ();
diff --git a/UnitTests/Views/ButtonTests.cs b/UnitTests/Views/ButtonTests.cs
index 0202db0e3b..5ef99cfa80 100644
--- a/UnitTests/Views/ButtonTests.cs
+++ b/UnitTests/Views/ButtonTests.cs
@@ -585,5 +585,21 @@ public void Pos_Center_Layout_AutoSize_False ()
TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
}
+
+ [Fact, AutoInitShutdown]
+ public void IsDefault_True_Does_Not_Get_The_Focus_On_Enter_Key ()
+ {
+ var wasClicked = false;
+ var view = new View { CanFocus = true };
+ var btn = new Button { Text = "Ok", IsDefault = true };
+ btn.Clicked += () => wasClicked = true;
+ Application.Top.Add (view, btn);
+ Application.Begin (Application.Top);
+ Assert.True (view.HasFocus);
+
+ Application.Top.ProcessColdKey (new KeyEvent (Key.Enter, new KeyModifiers ()));
+ Assert.True (view.HasFocus);
+ Assert.True (wasClicked);
+ }
}
}
diff --git a/testenvironments.json b/testenvironments.json
index 898ac827de..25324739d6 100644
--- a/testenvironments.json
+++ b/testenvironments.json
@@ -10,6 +10,11 @@
"type": "wsl",
"wslDistribution": "Ubuntu"
},
+ {
+ "name": "WSL-Ubuntu-20.04",
+ "type": "wsl",
+ "wslDistribution": "Ubuntu-20.04"
+ },
{
"name": "WSL-Debian",
"type": "wsl",