Console.ReadKey not working when debugging C# .Net

Hi,

I am porting a .NET core console application to Torizon.

The original application uses Console.ReadKey() to take commands from the user. This works fine when running in release but fails when debuging.

I suspect this is because of all the ssh magic that is happening when debuging the remote container.

The simple snipet below reproduces the issue:

using System;

namespace test_terminal
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            var i_KeyInfo = Console.ReadKey(true);
            Console.WriteLine(i_KeyInfo.Key);

        }
    }
}

I get the following error:

Exception has occurred: CLR/System.InvalidOperationException

An unhandled exception of type 'System.InvalidOperationException' occurred in System.Console.dll: 'Cannot read keys when either application does not have a console or when console input has been redirected. Try Console.Read.'

I did some research online and I found some sources that indicated that I should change the launch.json configuration to fix this.
So, I changed configurations.console to integratedTerminal from internalConsole.

I can’t notice any change difference after this change.

This are the contents of torizon-vscode-settings.yml

label: .NET Console Application
detail: >-
  $(terminal) Creates a minimal console application sample, based on a debian
  container and .NET Core 3.1
tags:
  - console
testing:
  title: .NET 5.0
  createCmd: torizon.createDotNetCoreApp
  createSelection: .NET Console Application
  projName: DotnetConsoleApp
  platformDistroCodeName: bullseye
  baseContainer: dotnet-50_bullseye
  mainSourceFile: Program.cs
  mainSourceFileBreakPoint: 9
  programOutput: Hello World
  hasSDKContainer: false
  stopOnEntry: false
extversion: 1.3.0

Am I doing something wrong ? Or is this simply not supported when debugging in Torizon ?

Thanks,
Jaume

Hi @jcabecerans !

On your error output we have Try Console.Read.

Have you tried it?

What is the result?

Best regards,

Hi @henrique.tx,

Console.Read only gives you an int that describes the character you read.
Console.ReadKey gives you an int plus more information such as :

 Shift, Alt, or Ctrl modifier keys was pressed simultaneously with the console key.

Additionally, I did try Console.Read(), it doesn’t crarsh. But it is not clear to me where is the terminal I should be typing in (in vscode).

The program I am porting uses the key modifiers to trigger different functionality, so I need this information.

Thanks,
Jaume

Hi @jcabecerans !

As it turns out, for now unfortunately there is no way of using VS Code to debug stdin usage.

So, you will need to debug this part of your application in some other way.


A nice (and recommended) way of developing is to use automated (unit) test frameworks (regardless of being able to actually debug something). Even more for user input cases.

So I would go down this path and prepare some tests for the input.

Also, you can always export the docker-compose file or the docker command-line to run your container directly on the module to be able to perform user input.

Best regards,