From bfbf2c40598ca25cc057f7eec3ec49a3969d9ac9 Mon Sep 17 00:00:00 2001 From: Friedrich von Never Date: Sun, 4 Oct 2020 23:28:43 +0700 Subject: [PATCH] Fix #1393: avoid dead key check for known unambiguous control characters --- PSReadLine/Keys.cs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/PSReadLine/Keys.cs b/PSReadLine/Keys.cs index 4a621bb2a..73bf9cf59 100644 --- a/PSReadLine/Keys.cs +++ b/PSReadLine/Keys.cs @@ -115,13 +115,21 @@ public override int GetHashCode() public static extern uint MapVirtualKey(ConsoleKey uCode, uint uMapType); [DllImport("user32.dll", CharSet = CharSet.Unicode)] - public static extern int ToUnicode( + public static extern int ToUnicodeEx( ConsoleKey uVirtKey, uint uScanCode, byte[] lpKeyState, [MarshalAs(UnmanagedType.LPArray)] [Out] char[] chars, int charMaxCount, - uint flags); + uint flags, + IntPtr dwhkl); + + [DllImport("user32.dll", CharSet = CharSet.Unicode)] + + private static extern IntPtr LoadKeyboardLayoutW( + string pwszKLID, + uint Flags + ); static readonly ThreadLocal toUnicodeBuffer = new ThreadLocal(() => new char[2]); static readonly ThreadLocal toUnicodeStateBuffer = new ThreadLocal(() => new byte[256]); @@ -147,7 +155,9 @@ internal static void TryGetCharFromConsoleKey(ConsoleKeyInfo key, ref char resul { flags |= (1 << 2); /* If bit 2 is set, keyboard state is not changed (Windows 10, version 1607 and newer) */ } - int charCount = ToUnicode(virtualKey, scanCode, state, chars, chars.Length, flags); + + var kl = LoadKeyboardLayoutW("00000409", 0); // US English keyboard layout + int charCount = ToUnicodeEx(virtualKey, scanCode, state, chars, chars.Length, flags, kl); if (charCount == 1) {