diff --git a/Terminal.Gui/Views/Shortcut.cs b/Terminal.Gui/Views/Shortcut.cs
index 641d68295a..589bb89de0 100644
--- a/Terminal.Gui/Views/Shortcut.cs
+++ b/Terminal.Gui/Views/Shortcut.cs
@@ -805,11 +805,6 @@ private void SetKeyViewDefaultLayout ()
private void UpdateKeyBindings (Key oldKey)
{
- if (!Key.IsValid)
- {
- return;
- }
-
if (BindKeyToApplication)
{
if (oldKey != Key.Empty)
@@ -819,8 +814,11 @@ private void UpdateKeyBindings (Key oldKey)
App?.Keyboard.KeyBindings.Remove (Key);
- // Use the form of Add that provides target since this is an app-level hotkey
- App?.Keyboard.KeyBindings.AddApp (Key, this, Command.HotKey);
+ if (Key != Key.Empty)
+ {
+ // Use the form of Add that provides target since this is an app-level hotkey
+ App?.Keyboard.KeyBindings.AddApp (Key, this, Command.HotKey);
+ }
}
else
{
@@ -830,7 +828,11 @@ private void UpdateKeyBindings (Key oldKey)
}
HotKeyBindings.Remove (Key);
- HotKeyBindings.Add (Key, Command.HotKey);
+
+ if (Key != Key.Empty)
+ {
+ HotKeyBindings.Add (Key, Command.HotKey);
+ }
}
}
diff --git a/Tests/UnitTestsParallelizable/Views/ShortcutTests.KeyDown.cs b/Tests/UnitTestsParallelizable/Views/ShortcutTests.KeyDown.cs
index 906fb8c04a..9866371f3a 100644
--- a/Tests/UnitTestsParallelizable/Views/ShortcutTests.KeyDown.cs
+++ b/Tests/UnitTestsParallelizable/Views/ShortcutTests.KeyDown.cs
@@ -5,6 +5,67 @@ namespace ViewsTests;
[TestSubject (typeof (Shortcut))]
public partial class ShortcutTests
{
+ // Copilot
+ ///
+ /// Verifies that setting to removes the key binding,
+ /// so the previously-bound key no longer triggers the action.
+ ///
+ [Fact]
+ public void Setting_Key_To_Empty_Removes_KeyBinding ()
+ {
+ IApplication app = Application.Create ();
+ Runnable runnable = new ();
+ app.Begin (runnable);
+
+ var action = 0;
+
+ Shortcut shortcut = new () { Key = Key.F10, Title = "Test", Action = () => action++ };
+
+ runnable.Add (shortcut);
+
+ app.Keyboard.RaiseKeyDownEvent (Key.F10);
+ Assert.Equal (1, action);
+
+ shortcut.Key = Key.Empty;
+
+ app.Keyboard.RaiseKeyDownEvent (Key.F10);
+ Assert.Equal (1, action); // Should not have incremented; old binding must be gone
+ }
+
+ // Copilot
+ ///
+ /// Verifies that setting to removes the
+ /// application-level key binding when is .
+ ///
+ [Fact]
+ public void Setting_Key_To_Empty_Removes_AppLevel_KeyBinding ()
+ {
+ IApplication app = Application.Create ();
+ Runnable runnable = new ();
+ app.Begin (runnable);
+
+ var action = 0;
+
+ Shortcut shortcut = new ()
+ {
+ App = app,
+ Key = Key.F10,
+ Title = "Test",
+ BindKeyToApplication = true,
+ Action = () => action++
+ };
+
+ runnable.Add (shortcut);
+
+ app.Keyboard.RaiseKeyDownEvent (Key.F10);
+ Assert.Equal (1, action);
+
+ shortcut.Key = Key.Empty;
+
+ app.Keyboard.RaiseKeyDownEvent (Key.F10);
+ Assert.Equal (1, action); // Should not have incremented; old app binding must be gone
+ }
+
// Claude - Opus 4.6
///
/// Verifies that pressing various keys invokes the delegate.
@@ -111,15 +172,15 @@ public void KeyDown_Key_Raises_HandlingHotKey_And_Accepting (bool canFocus)
var accepting = 0;
- shortcut.Accepting += (_, e) => { accepting++; };
+ shortcut.Accepting += (_, _) => { accepting++; };
var activated = 0;
- shortcut.Activating += (_, e) => { activated++; };
+ shortcut.Activating += (_, _) => { activated++; };
var handlingHotKey = 0;
- shortcut.HandlingHotKey += (_, e) => { handlingHotKey++; };
+ shortcut.HandlingHotKey += (_, _) => { handlingHotKey++; };
app.Keyboard.RaiseKeyDownEvent (shortcut.Key);
@@ -143,15 +204,15 @@ public void CheckBox_KeyDown_Key_Raises_HandlingHotKey_And_Accepting (bool canFo
var accepting = 0;
- shortcut.Accepting += (_, e) => { accepting++; };
+ shortcut.Accepting += (_, _) => { accepting++; };
var activated = 0;
- shortcut.Activating += (_, e) => { activated++; };
+ shortcut.Activating += (_, _) => { activated++; };
var handlingHotKey = 0;
- shortcut.HandlingHotKey += (_, e) => { handlingHotKey++; };
+ shortcut.HandlingHotKey += (_, _) => { handlingHotKey++; };
app.Keyboard.RaiseKeyDownEvent (shortcut.Key);
@@ -192,30 +253,11 @@ public void KeyDown_Valid_Keys_Raises_Accepted_Activated_Correctly (bool canFocu
var accepting = 0;
- shortcut.Accepting += (_, e) =>
- {
- accepting++;
-
- //e.Handled = true;
- };
+ shortcut.Accepting += (_, _) => { accepting++; };
var activated = 0;
- shortcut.Activating += (_, e) =>
- {
- activated++;
-
- //e.Handled = true;
- };
-
- var handlingHotKey = 0;
-
- shortcut.HandlingHotKey += (_, e) =>
- {
- handlingHotKey++;
-
- //e.Handled = true;
- };
+ shortcut.Activating += (_, _) => { activated++; };
app.Keyboard.RaiseKeyDownEvent (key);
@@ -223,7 +265,6 @@ public void KeyDown_Valid_Keys_Raises_Accepted_Activated_Correctly (bool canFocu
Assert.Equal (expectedActivate, activated);
}
-
[Fact]
public void Mouse_Click_On_CommandView_Causes_Activation ()
{
@@ -255,7 +296,8 @@ public void Mouse_Click_On_CommandView_Causes_Activation ()
// Act & Assert - Click at various X positions across the entire CommandView
Rectangle screen = shortcut.CommandView.FrameToScreen ();
- for (var x = screen.X; x < screen.X + screen.Width; x++)
+
+ for (int x = screen.X; x < screen.X + screen.Width; x++)
{
int expectedCount = activatingCount + 1;
@@ -298,7 +340,8 @@ public void Mouse_Click_On_HelpView_Causes_Activation ()
// Act & Assert - Click at various X positions across the entire HelpView
Rectangle screen = shortcut.HelpView.FrameToScreen ();
- for (var x = screen.X; x < screen.X + screen.Width; x++)
+
+ for (int x = screen.X; x < screen.X + screen.Width; x++)
{
int expectedCount = activatingCount + 1;
@@ -310,7 +353,6 @@ public void Mouse_Click_On_HelpView_Causes_Activation ()
}
}
-
[Fact]
public void Mouse_Click_On_KeyView_Causes_Activation ()
{
@@ -342,7 +384,8 @@ public void Mouse_Click_On_KeyView_Causes_Activation ()
// Act & Assert - Click at various X positions across the entire KeyView
Rectangle screen = shortcut.KeyView.FrameToScreen ();
- for (var x = screen.X+1; x < screen.X + screen.Width; x++)
+
+ for (int x = screen.X + 1; x < screen.X + screen.Width; x++)
{
int expectedCount = activatingCount + 1;
@@ -453,5 +496,4 @@ public void Mouse_Click_Button_CommandView_Raises_Accepting_On_Both (bool comman
Assert.Equal (0, buttonActivatingCount);
Assert.Equal (0, shortcutActivatingCount);
}
-
}