Get a character from the console without echo (_getch) or with echo (_getche).
int _getch( void );
int _getche( void );
Routine | Required Header | Compatibility |
_getch | <conio.h> | Win 95, Win NT |
_getche | <conio.h> | Win 95, Win NT |
For additional compatibility information, see Compatibility in the Introduction.
Libraries
LIBC.LIB | Single thread static library, retail version |
LIBCMT.LIB | Multithread static library, retail version |
MSVCRT.LIB | Import library for MSVCRT.DLL, retail version |
Return Value
Both _getch and _getche return the character read. There is no error return.
Remarks
The _getch function reads a single character from the console without echoing. _getche 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 an arrow key, _getch and _getche must be called twice; the first call returns 0 or 0xE0, and the second call returns the actual key code.
Example
/* 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 */
}
Output
Type 'Y' when finished typing keys: Y