TreatControlCAsInput issue. Is this a bug?
Asked Answered
I

2

12

Just ran across the problem described below. If "Console.TreatControlCAsInput = true;", you have to press [enter] twice on ReadLine().

I've written some demo code below. I am correct in surmising that this code demonstrate a bug in the .NET 4 framework?

        Console.Write("Test 1: Console.TreatControlCAsInput = false\nType \"hello\": ");
        {
            string readline = Console.ReadLine(); // type "hello" [enter].
            Console.WriteLine("You typed: {0}", readline);
            // Prints "hello".
        }

        Console.Write("Test 2: Console.TreatControlCAsInput = true\nType \"hello\": ");
        Console.TreatControlCAsInput = true;
        {
            string readline = Console.ReadLine(); // type "hello" [enter].
            Console.WriteLine("You typed: {0}", readline);
            // Should print "hello" - but instead, you have to press [enter] 
            // *twice* to complete the ReadLine() command, and it adds a "\r" 
            // rather than a "\n" to the output (so it overwrites the original line)
        }

        // This bug is a fatal error, because it makes all ReadLine() commands unusable.

        Console.Write("[any key to exit]");
        Console.ReadKey();
Iranian answered 17/11, 2011 at 14:28 Comment(4)
What platform are you running this on?Jair
I could reproduce this on Windows 7. Enter seems be doing the function of the HOME buttonCheddar
I see it reported in 2006 where is is marked as closed, by design. connect.microsoft.com/VisualStudio/feedback/details/226101/…Crenshaw
@parapura rajkumar Actually, it seems to be printing \r (carriage return) instead of \n (line feed) when [enter] is pressed, and also requiring two [enter]s to be pressed to complete the ReadLine().Iranian
A
14

It is a known issue with the Windows Console subsystem and has been reported on Microsoft Connect back in 2006.

Posted by Microsoft on 22/05/2007 at 12:37

Hello ARos, Thank you for reporting this issue in System.Console. The behavior exists with the Windows Console subsystem, as demonstrated with the attached Win32 C application. I have reported the issue to the Windows Console subsystem owner.

Thanks, Josh

Aliphatic answered 17/11, 2011 at 14:35 Comment(2)
As a side note: that connect item was closed as "by design". So Sounds like they decided it was a feature.Androclinium
@Chris Lively Amazing "feature" - it breaks ReadLine() so its unusable. I wonder how many developers have collided with this particular problem.Iranian
H
3

Not a bug on the Framework but it looks like a bug in the Windows console subsystem.

Handclasp answered 17/11, 2011 at 14:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.