Skip to content

Commit d54cf58

Browse files
committed
(#1393) Address the PR feedback
1 parent 40a3868 commit d54cf58

File tree

3 files changed

+31
-100
lines changed

3 files changed

+31
-100
lines changed

PSReadLine/Keys.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,7 @@ internal static void TryGetCharFromConsoleKey(ConsoleKeyInfo key, ref char resul
156156
flags |= (1 << 2); /* If bit 2 is set, keyboard state is not changed (Windows 10, version 1607 and newer) */
157157
}
158158

159-
var layout = WindowsKeyboardLayoutUtil.GetConsoleKeyboardLayout()
160-
?? WindowsKeyboardLayoutUtil.GetConsoleKeyboardLayoutFallback();
159+
IntPtr layout = PlatformWindows.GetConsoleKeyboardLayout();
161160

162161
int charCount = ToUnicodeEx(virtualKey, scanCode, state, chars, chars.Length, flags, layout);
163162

PSReadLine/PlatformWindows.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,4 +1015,34 @@ private static void TerminateStragglers()
10151015
}
10161016
}
10171017
}
1018+
1019+
/// <remarks>
1020+
/// This method helps to find the active keyboard layout in a terminal process that controls the current console
1021+
/// application. For now, we assume that at the moment when we are asked to process a keyboard shortcut, the
1022+
/// process owning the foreground window has to be the terminal process controlling the current console.
1023+
/// </remarks>
1024+
public static IntPtr GetConsoleKeyboardLayout()
1025+
{
1026+
IntPtr foregroundWindow = GetForegroundWindow();
1027+
if (foregroundWindow != IntPtr.Zero)
1028+
{
1029+
uint tid = GetWindowThreadProcessId(foregroundWindow, out _);
1030+
if (tid != 0)
1031+
{
1032+
return GetKeyboardLayout(tid);
1033+
}
1034+
}
1035+
1036+
// Fall back to the default keyboard when we failed to find the parent terminal process.
1037+
return GetKeyboardLayout(0);
1038+
}
1039+
1040+
[DllImport("user32.dll")]
1041+
private static extern IntPtr GetForegroundWindow();
1042+
1043+
[DllImport("User32.dll", SetLastError = true)]
1044+
private static extern IntPtr GetKeyboardLayout(uint idThread);
1045+
1046+
[DllImport("user32.dll", SetLastError = true)]
1047+
static extern uint GetWindowThreadProcessId(IntPtr hwnd, out IntPtr proccess);
10181048
}

PSReadLine/WindowsKeyboardLayoutUtil.cs

Lines changed: 0 additions & 98 deletions
This file was deleted.

0 commit comments

Comments
 (0)