1

I'm making a basic console app.
I basically want to wait for a particular key press like F1, F2 ... or if non is pressed, the user can type in the entire command. And the problem I'm facing is that when I call Console.ReadKey() it always suppresses the first character in the user's input. This will break the console history and the command itself. I want to read only functional keys, but when the user wants to type a normal command like 'echo' i don't want the 'e' to be treated as a keypress but as an entire command.
Examples:

  • User presses F1 which means they don't want to input a command so the F1 logic will execute and the user will be able to make a new interaction (command or key press)

  • User presses 'e' which means they want to type an entire command like 'echo' so the program will wait for a next line (enter)

    Code

         public string[] Input()
         {
             Console.Write(Core.commandChar);
             var KP = Console.ReadKey();
    
             if (KP.Key == ConsoleKey.F1)
             {
                 inputs = new string[] { "echo", "test", "" };
                 return inputs;
             }
    
    
             inputs = (KP.KeyChar+Console.ReadLine()).Split(' ');
             return inputs;
         }
    

Is there a way to pull this off?

Lexa555
  • 11
  • 2
  • In your example it sounds like you need to keep hold of the `"e"` and prepend the result of `Console.ReadLine()` `"cho"` with the `"e"` to get the full command `"echo"` – phuzi Jul 26 '22 at 21:16
  • 1
    Add your code to your question. `Console.ReadKey()` doesn't "suppress" anything. – 3Dave Jul 26 '22 at 21:16
  • 1
    Sure. Use ReadKey(), if the character is an FX key then do action otherwise add it to a command string and read another ReadKey until you have a functional string or Enter is pressed – Igor Jul 26 '22 at 21:17
  • 1
    Seems your application needs some state management to know whether to run a command or print the keypress and expect more inputs until ENTER is pressed (and then parse & run some other command). Having your code in the post would probably help us help you here. – Mathieu Guindon Jul 26 '22 at 21:20
  • I tried to hold `"e"` and add `"cho"` but this makes it impossible to delete the first character and also it is no longer possible to recall the command with up arrow. – Lexa555 Jul 26 '22 at 21:24

0 Answers0