From 08e6a86e83a201f4e8f1c8a74ea8d58d008c182f Mon Sep 17 00:00:00 2001 From: GermCatt Date: Mon, 16 Nov 2026 20:11:08 +0300 Subject: [PATCH 1/2] Preserve Ctrl-letter control characters during key normalization Keep incoming Control-without-Alt characters U+0001 through U+001A from being translated through a stale Windows keyboard layout. Add regression and compatibility tests for key normalization. --- PSReadLine/Keys.cs | 7 +- test/ControlCharacterTests.cs | 132 ++++++++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 test/ControlCharacterTests.cs diff --git a/PSReadLine/Keys.cs b/PSReadLine/Keys.cs index 70cd80e47..6a74fd796 100644 --- a/PSReadLine/Keys.cs +++ b/PSReadLine/Keys.cs @@ -245,7 +245,12 @@ void AppendPart(string str) // another special key, such as 'Ctrl+?' and 'Ctrl+;'. isDeadKey = (c == '\0') && (consoleKey >= ConsoleKey.Oem1 && consoleKey <= ConsoleKey.Oem102) && !isCtrl; - if (!isDeadKey) + // Ctrl+A through Ctrl+Z already carry an unambiguous control character. + // Translating the virtual key without Ctrl can replace it with a letter + // from a stale input-thread layout (for example Ctrl+V becomes Ctrl+м). + // Keep Alt combinations on the layout-dependent path for AltGr handling. + bool isControlLetter = isCtrl && !isAlt && c >= '\x01' && c <= '\x1a'; + if (!isDeadKey && !isControlLetter) { // A dead key could pass the above heuristic check, such as 'Shift+6' in US-INTL keyboard, which represents the // diacritic '^' and generates 'D6' ConsoleKey, '\0' key char and 'Shift' modifier. diff --git a/test/ControlCharacterTests.cs b/test/ControlCharacterTests.cs new file mode 100644 index 000000000..b361d6cc3 --- /dev/null +++ b/test/ControlCharacterTests.cs @@ -0,0 +1,132 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the 2-Clause BSD License. + +using System; +using System.Collections.Generic; +using System.Runtime.ExceptionServices; +using System.Runtime.InteropServices; +using System.Threading; +using Microsoft.PowerShell; +using Xunit; + +namespace Test +{ + public class ControlCharacterTests + { + [DllImport("user32.dll")] + private static extern int GetKeyboardLayoutList(int count, [Out] IntPtr[] layouts); + + [DllImport("user32.dll")] + private static extern IntPtr GetKeyboardLayout(uint threadId); + + [DllImport("user32.dll")] + private static extern IntPtr ActivateKeyboardLayout(IntPtr layout, uint flags); + + public static IEnumerable ControlLetters() + { + for (int code = 1; code <= 26; code++) + { + yield return new object[] { code, false }; + yield return new object[] { code, true }; + } + } + + // Use only an already installed layout, on a disposable worker thread. + // No foreground-window messages or global input-language changes. + private static void WithRussianLayout(Action action) + { + Skip.IfNot(RuntimeInformation.IsOSPlatform(OSPlatform.Windows), "Requires Windows keyboard layouts."); + var layouts = new IntPtr[GetKeyboardLayoutList(0, null)]; + GetKeyboardLayoutList(layouts.Length, layouts); + var russian = Array.Find(layouts, p => (p.ToInt64() & 0xffff) == 0x0419); + Skip.If(russian == IntPtr.Zero, "Russian keyboard layout is not installed."); + Exception failure = null; + var worker = new Thread(() => + { + var original = GetKeyboardLayout(0); + try + { + Assert.NotEqual(IntPtr.Zero, ActivateKeyboardLayout(russian, 0)); + // A terminal-owner thread may override the current thread's layout. + Skip.If((PlatformWindows.GetConsoleKeyboardLayout().ToInt64() & 0xffff) != 0x0419, + "PSReadLine selects another terminal thread's layout."); + action(); + } + catch (Exception ex) { failure = ex; } + finally { ActivateKeyboardLayout(original, 0); } + }); + worker.Start(); + worker.Join(); + if (failure != null) ExceptionDispatchInfo.Capture(failure).Throw(); + } + + [Theory] + [MemberData(nameof(ControlLetters))] + public void ControlCharactersRetainTheirMeaning(int code, bool shift) + { + var input = new ConsoleKeyInfo((char)code, (ConsoleKey)('A' + code - 1), shift, false, true); + var key = PSKeyInfo.FromConsoleKeyInfo(input); + Assert.Equal("Ctrl+" + (char)((shift ? 'A' : 'a') + code - 1), key.ToString()); + var roundTrip = key.AsConsoleKeyInfo(); + Assert.Equal(input.KeyChar, roundTrip.KeyChar); + Assert.Equal(input.Key, roundTrip.Key); + Assert.Equal(input.Modifiers, roundTrip.Modifiers); + } + + [SkippableTheory] + [InlineData(3, false)] + [InlineData(3, true)] + [InlineData(18, false)] + [InlineData(18, true)] + [InlineData(22, false)] + [InlineData(22, true)] + public void ControlCharactersRetainTheirMeaningWithRussianLayout(int code, bool shift) + { + WithRussianLayout(() => ControlCharactersRetainTheirMeaning(code, shift)); + } + + [Theory] + [InlineData('м', ConsoleKey.V, false, false, "м")] + [InlineData('м', ConsoleKey.V, true, false, "Ctrl+м")] + [InlineData('@', ConsoleKey.D2, true, true, "@")] + [InlineData('ó', ConsoleKey.O, true, true, "ó")] + public void PrintableCharactersKeepTheirMeaning(char character, ConsoleKey key, bool control, bool alt, string expected) + { + Assert.Equal(expected, PSKeyInfo.FromConsoleKeyInfo( + new ConsoleKeyInfo(character, key, false, alt, control)).ToString()); + } + + [Theory] + [InlineData('\t', ConsoleKey.Tab, "Ctrl+Tab")] + [InlineData('\r', ConsoleKey.Enter, "Ctrl+Enter")] + [InlineData('\b', ConsoleKey.Backspace, "Ctrl+Backspace")] + [InlineData('\x1b', ConsoleKey.Escape, "Ctrl+Escape")] + public void NamedKeysKeepTheirMeaning(char character, ConsoleKey key, string expected) + { + Assert.Equal(expected, PSKeyInfo.FromConsoleKeyInfo( + new ConsoleKeyInfo(character, key, false, false, true)).ToString()); + } + + [SkippableFact] + public void ControlAltAndPrintableInputKeepLayoutDependentMeaning() + { + WithRussianLayout(() => + { + Assert.Equal("Ctrl+Alt+м", PSKeyInfo.FromConsoleKeyInfo( + new ConsoleKeyInfo('\x16', ConsoleKey.V, false, true, true)).ToString()); + Assert.Equal("м", PSKeyInfo.FromConsoleKeyInfo( + new ConsoleKeyInfo('м', ConsoleKey.V, false, false, false)).ToString()); + Assert.Equal("@", PSKeyInfo.FromConsoleKeyInfo( + new ConsoleKeyInfo('@', ConsoleKey.D2, false, true, true)).ToString()); + // A missing character, a dead-key-shaped input, and a control character + // without the Control modifier must still follow the existing path. + Assert.Equal("Ctrl+к", PSKeyInfo.FromConsoleKeyInfo( + new ConsoleKeyInfo('\0', ConsoleKey.R, false, false, true)).ToString()); + Assert.Equal("\0", PSKeyInfo.FromConsoleKeyInfo( + new ConsoleKeyInfo('\0', ConsoleKey.Oem7, false, false, false)).ToString()); + Assert.Equal("м", PSKeyInfo.FromConsoleKeyInfo( + new ConsoleKeyInfo('\x16', ConsoleKey.V, false, false, false)).ToString()); + }); + } + } +} From 95a0f6f040aee489b59b6d088ca2eaa8da2b03a4 Mon Sep 17 00:00:00 2001 From: GermCatt Date: Mon, 16 Nov 2026 20:24:53 +0300 Subject: [PATCH 2/2] Run control-character regression tests in the standard test script Include the standalone control-character test class alongside each layout-specific test group so CI discovers the regression cases. --- tools/helper.psm1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/helper.psm1 b/tools/helper.psm1 index 56776ea9a..288f3017d 100644 --- a/tools/helper.psm1 +++ b/tools/helper.psm1 @@ -216,7 +216,7 @@ function Start-TestRun { $filter = if ($Layout) { Write-Log "`nTesting $Layout on $OperatingSystem..." - "FullyQualifiedName~Test.{0}_{1}" -f ($Layout -replace '-','_'), $OperatingSystem + "FullyQualifiedName~Test.{0}_{1}|FullyQualifiedName~Test.ControlCharacterTests" -f ($Layout -replace '-','_'), $OperatingSystem } else { ## Today, tests for screen-reader mode only run on Windows with the 'en-US' layout. $Layout = "screen-reader"