_getche_getwche

从具有回显的控制台获取字符。

重要

此 API 不能用于在 Windows 运行时中执行的应用程序。 有关详细信息,请参阅通用 Windows 平台应用中不支持的 CRT 函数

语法

int _getche( void );
wint_t _getwche( void );

返回值

返回读取的字符。 无错误返回。

注解

_getche_getwche 函数从具有回显的控制台读取单个字符,这意味着,字符会在控制台中显示。 无可用于读取 CTRL+C 的函数。 当 _getche_getwche 读取函数键或箭头键时,必须调用该函数两次;第一次调用返回 0 或 0xE0,第二次调用返回实际键代码。

这些函数会锁定调用线程,因此是线程安全的。 有关非锁定版本,请参阅 _getche_nolock_getwche_nolock

默认情况下,此函数的全局状态范围限定为应用程序。 若要更改此行为,请参阅 CRT 中的全局状态

一般文本例程映射

Tchar.h 例程 _UNICODE_MBCS 未定义 _MBCS 已定义 _UNICODE 已定义
_gettche _getche _getche _getwche

要求

例程 必需的标头
_getche <conio.h>
_getwche <conio.h> 或 <wchar.h>

有关兼容性的详细信息,请参阅 兼容性

示例

// crt_getche.c
// compile with: /c
// This program reads characters from
// the keyboard until it receives a 'Y' or 'y'.

#include <conio.h>
#include <ctype.h>

int main( void )
{
   int ch;

   _cputs( "Type 'Y' when finished typing keys: " );
   do
   {
      ch = _getche();
      ch = toupper( ch );
   } while( ch != 'Y' );

   _putch( ch );
   _putch( '\r' );    // Carriage return
   _putch( '\n' );    // Line feed
}
abcdefy
Type 'Y' when finished typing keys: abcdefyY

另请参阅

控制台和端口 I/O
_cgets_cgetws
getcgetwc
_ungetch_ungetwch_ungetch_nolock_ungetwch_nolock