runtime icon indicating copy to clipboard operation
runtime copied to clipboard

The first character of the line is not written in Console.Write or WriteLine if Esc was pressed

Open johuerta opened this issue 3 years ago • 3 comments

Description

Copied from: https://developercommunity.visualstudio.com/t/The-first-character-of-the-line-is-not-w/10111105

This reproduces outside of Visual Studio, I believe there might be some weird interaction going on with Windows Terminal.

Reproduction Steps

Use this C# code to reproduce the error.

static public void Main()
        {
			do
			{
				Console.Clear();
				Console.WriteLine("This is a text test. Press Esc to test and Enter to exit");

				ConsoleKey c = Console.ReadKey().Key;
				if (c == ConsoleKey.Enter) break;
			}
			while (true);
        }

You can also use Console.Write(…).

If Esc or Shift+Esc is pressed, the line is written without the first capital T. Other characters in the input do not have such effect. The cursor position of the console is (0, 0) before the WriteLine and (0, 1) after. I cannot debug the source code of WriteLine to see what happens inside, so please check this. I use VS 2022 Community, VisualStudio.17.Release/17.2.6+32630.192, .NET Framework 4.8.04084.

Expected behavior

The complete string is printed

			Console.WriteLine("This is a text test. Press Esc to test and Enter to exit");

Actual behavior

The first 'T' is not printed.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

johuerta avatar Apr 03 '23 17:04 johuerta

Tagging subscribers to this area: @dotnet/area-system-console See info in area-owners.md if you want to be subscribed.

Issue Details

Description

Copied from: https://developercommunity.visualstudio.com/t/The-first-character-of-the-line-is-not-w/10111105

This reproduces outside of Visual Studio, I believe there might be some weird interaction going on with Windows Terminal.

Reproduction Steps

Use this C# code to reproduce the error.

static public void Main() { do { Console.Clear(); Console.WriteLine("This is a text test. Press Esc to test and Enter to exit");

			ConsoleKey c = Console.ReadKey().Key;
			if (c == ConsoleKey.Enter) break;
		}
		while (true);
    }

You can also use Console.Write(…).

If Esc or Shift+Esc is pressed, the line is written without the first capital T. Other characters in the input do not have such effect. The cursor position of the console is (0, 0) before the WriteLine and (0, 1) after. I cannot debug the source code of WriteLine to see what happens inside, so please check this. I use VS 2022 Community, VisualStudio.17.Release/17.2.6+32630.192, .NET Framework 4.8.04084.

Expected behavior

The complete string is printed

			Console.WriteLine("This is a text test. Press Esc to test and Enter to exit");

Actual behavior

The first 'T' is not printed.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Author: johuerta
Assignees: -
Labels:

area-System.Console

Milestone: -

ghost avatar Apr 03 '23 17:04 ghost

I believe this happens because the pressed key is printed to the console. And when the ESC character is printed to the Windows console, strange things happen.

As an example, try running Console.WriteLine("\x1BMHello world!");. Then try running Console.ReadKey(); Console.ReadKey(); and pressing the sequence ESC, M and observe that the same thing happens (the cursor moves one line up).

If this is not what you want, you can use Console.ReadKey(intercept: true); instead.

svick avatar Apr 07 '23 11:04 svick

@svick yes, I can see adding the 'intercept:true' makes this behavior go away. Sounds like it is expected behavior then.

johuerta avatar Apr 10 '23 20:04 johuerta