Get a character from the console without echo (_getch) or with echo (_getche).
#include <conio.h> | Required only for function declarations |
int _getch( void );
int _getche( void );
The _getch function reads a single character from the console without echoing. The _getche function reads a single character from the console and echoes the character read. Neither function can be used to read CTRL+C.
When reading a function key or cursor-moving key, the _getch and _getche functions must be called twice; the first call returns 0 or 0xE0, and the second call returns the actual key code.
Both the _getch and _getche functions return the character read. There is no error return.
Standards:None
16-Bit:DOS, QWIN, WIN, WIN DLL
32-Bit:DOS32X
/* GETCH.C: This program reads characters from the keyboard until it
* receives a 'Y' or 'y'.
*/
#include <conio.h>
#include <ctype.h>
void main( void )
{
int ch;
_cputs( "Type 'Y' when finished typing keys: " );
do
{
ch = _getch();
ch = toupper( ch );
} while( ch != 'Y' );
_putch( ch );
_putch( '\r' ); /* Carriage return */
_putch( '\n' ); /* Line feed */
}
Type 'Y' when finished typing keys: Y