Skip to content

Commit 721d388

Browse files
BDispCopilot
andauthored
Fixes #5453. Fix Windows VT input encoding for IME and non-ASCII characters (#5454)
* Fixes #5453. Fix Windows VT input encoding for IME and non-ASCII characters * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * Cleanup code * Apply suggested changes related with review feedback --------- Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent 57491dc commit 721d388

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

Terminal.Gui/Drivers/WindowsHelpers/WindowsVTInputHelper.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ internal sealed class WindowsVTInputHelper : IDisposable
5151
[DllImport ("kernel32.dll")]
5252
private static extern uint GetConsoleCP ();
5353

54+
[DllImport ("kernel32.dll", SetLastError = true)]
55+
private static extern bool SetConsoleCP (uint wCodePageID);
56+
5457
#endregion
5558

5659
private const int ERROR_NOT_FOUND = 1168;
@@ -68,6 +71,22 @@ internal sealed class WindowsVTInputHelper : IDisposable
6871
private uint _originalConsoleMode;
6972
private bool _disposed;
7073

74+
private const uint CP_UTF8 = 65001;
75+
76+
private readonly uint _originalConsoleCP;
77+
78+
public WindowsVTInputHelper ()
79+
{
80+
_originalConsoleCP = GetConsoleCP ();
81+
82+
if (_originalConsoleCP == CP_UTF8 || SetConsoleCP (CP_UTF8))
83+
{
84+
return;
85+
}
86+
int error = Marshal.GetLastWin32Error ();
87+
Logging.Warning ($"{nameof (WindowsVTInputHelper)}: Failed to set console input code page to UTF-8. Win32 error: {error}");
88+
}
89+
7190
/// <summary>
7291
/// Gets whether VTS input mode was successfully enabled.
7392
/// </summary>
@@ -238,6 +257,7 @@ public void Restore ()
238257

239258
try
240259
{
260+
SetConsoleCP (_originalConsoleCP);
241261
SetConsoleMode (InputHandle, _originalConsoleMode);
242262
IsEnabled = false;
243263
}

0 commit comments

Comments
 (0)