From 83a395a162a5c38b899234282996ffafb106daf0 Mon Sep 17 00:00:00 2001 From: BDisp Date: Wed, 22 Apr 2026 21:36:31 +0100 Subject: [PATCH] Fixes #5057. Pressing `Alt-T` in a `TextField` causes a `t` to be entered --- .../AnsiHandling/KittyKeyboardPattern.cs | 12 +++++++++++- .../AnsiHandling/KittyKeyboardParsingTests.cs | 13 +++++++++++++ .../Views/TextFieldTests.cs | 19 +++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/Terminal.Gui/Drivers/AnsiHandling/KittyKeyboardPattern.cs b/Terminal.Gui/Drivers/AnsiHandling/KittyKeyboardPattern.cs index 925849d8f8..25182559c7 100644 --- a/Terminal.Gui/Drivers/AnsiHandling/KittyKeyboardPattern.cs +++ b/Terminal.Gui/Drivers/AnsiHandling/KittyKeyboardPattern.cs @@ -128,7 +128,17 @@ public class KittyKeyboardPattern : AnsiKeyboardParserPattern (key, modifierField) = NormalizeShiftedPrintableKey (key, modifierField); } - return string.IsNullOrEmpty (modifierField) ? key : ApplyModifiersAndEventType (modifierField, key); + if (!string.IsNullOrEmpty (modifierField)) + { + key = ApplyModifiersAndEventType (modifierField, key); + } + + if ((key.IsAlt || key.IsCtrl) && !string.IsNullOrEmpty (key.AssociatedText)) + { + key = new Key (key) { AssociatedText = string.Empty }; + } + + return key; } private static string ParseAssociatedText (string textField) diff --git a/Tests/UnitTestsParallelizable/Drivers/AnsiHandling/KittyKeyboardParsingTests.cs b/Tests/UnitTestsParallelizable/Drivers/AnsiHandling/KittyKeyboardParsingTests.cs index 6ba6d25680..b5aea2dde8 100644 --- a/Tests/UnitTestsParallelizable/Drivers/AnsiHandling/KittyKeyboardParsingTests.cs +++ b/Tests/UnitTestsParallelizable/Drivers/AnsiHandling/KittyKeyboardParsingTests.cs @@ -499,6 +499,19 @@ public void KittyPattern_AssociatedText_ShiftedPrintableKey () Assert.Equal ("!", key.GetPrintableText ()); } + [Fact] + public void KittyPattern_AssociatedText_AltModifiedPrintableKey_IsSuppressed () + { + // ESC[116;3;116u = Alt+t with associated text 't' + Key? key = _pattern.GetKey ("\u001b[116;3;116u"); + + Assert.NotNull (key); + Assert.True (key.IsAlt); + Assert.Equal (Key.T.WithAlt.KeyCode, key.KeyCode); + Assert.Equal (string.Empty, key.AssociatedText); + Assert.Equal (string.Empty, key.GetPrintableText ()); + } + [Fact] public void KittyPattern_AssociatedText_MultipleCodePoints () { diff --git a/Tests/UnitTestsParallelizable/Views/TextFieldTests.cs b/Tests/UnitTestsParallelizable/Views/TextFieldTests.cs index 9098025415..58a7854f50 100644 --- a/Tests/UnitTestsParallelizable/Views/TextFieldTests.cs +++ b/Tests/UnitTestsParallelizable/Views/TextFieldTests.cs @@ -355,6 +355,25 @@ public void KittyAltGr2_InsertsAtSign () top.Dispose (); } + [Fact] + public void KittyAltModifiedPrintableKey_DoesNotInsertAssociatedText () + { + Runnable top = new (); + TextField tf = new () { Width = 10 }; + top.Add (tf); + tf.SetFocus (); + tf.ClearAllSelection (); + tf.InsertionPoint = 0; + + Key? key = new KittyKeyboardPattern ().GetKey ("\u001b[116;3;116u"); + + Assert.NotNull (key); + Assert.False (top.NewKeyDownEvent (key)); + Assert.Equal (string.Empty, tf.Text); + + top.Dispose (); + } + [Fact] public void ShiftedDigitKey_WithoutKittyMetadata_InsertsBaseDigit () {