Follow the "press any key" procedure that is compatible with redirected standard input

I have a C / C ++ program that runs in the console window and prompts the user to press any key to continue, or “q” to exit. Currently, _kbhit () is used to determine if a key has been pressed, and then uses _getch () to determine what it was.

Now I want to be able to automate this program (from a C # application using a process with the RedirectStandardInput and CreateNoWindow parameters). Obviously, I can no longer rely on _kbhit (), as it uses ReadConsoleInput (), which does not work when launched using my C # application. In my C # application, I use process.StandardInput.Write ("A") to insert something into the stream to continue the console application.

In the console application, I used SetConsoleMode () to clear the ENABLE_LINE_INPUT flag so that I can use getchar () to return as soon as the character is pressed, and this works quite well (when I press the character key in the console window, and also when the call is made from C # applications). However, it has drawbacks in that now it only accepts characters (for example, F, Alt, Shift, etc.), which is not a big problem, but it seems to me that I need to press the return button twice ( and this is the key that many people will probably prefer to click "in any situation").

Does anyone know how I can make a console application respond to a key (any bonus, charcters and return only is acceptable) ONCE is pressed, but still responding to a single character pressed on a stream from my C # application?

-, , ( "" ) , , , , "q". , PAUSE, : (.

, , . !

+3
4

:

  • C, ++ . (). *
  • C ++ -. , C ++.

++ " Enter " ignore cin:

void Pause(void)
{
    std::cout << "Press ENTER to continue.\n";
    std::cout.flush(); // Insurance, make sure the text is displayed.
    std::cin.ignore(100000, '\n');  // Ignore characters until an ENTER (newline) is received.
    return;
}

Pause. .

  • , C ++, . . , , . , - .
+2

cin.get(). .

0

, !

-, , , , , . , _kbhit(), PeekNamedPipe(), , #, stdin. , , , , .

, , , , :)

0

Source: https://habr.com/ru/post/1753557/


All Articles