_getche, _getwche

在有回應的情形下,從主控台取得字元。

重要

這個 API 不能用於在 Windows 執行階段中執行的應用程式。 如需詳細資訊,請參閱 CRT functions not supported in Universal Windows Platform apps (通用 Windows 平台應用程式中不支援的 CRT 函式)。

語法

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

傳回值

傳回讀取的字元。 沒有傳回錯誤。

備註

_getche_getwche 函式在有回應的情形下,從主控台讀取單一字元,表示該字元會顯示在主控台上。 這些函式都不可以用來讀取 CTRL+C。 當或 _getwche 讀取函式索引鍵或方向鍵時 _getche ,必須呼叫函式兩次;第一次呼叫會傳回 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
getc, getwc
_ungetch, _ungetwch, _ungetch_nolock, _ungetwch_nolock