_getch, _getwch

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

重要

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

語法

int _getch( void );
wint_t _getwch( void );

傳回值

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

備註

_getwch_getch 式會從主控台讀取單一字元,而不會回應字元。 若要讀取函式索引鍵或方向鍵,每個函式都必須呼叫兩次。 第一個呼叫會傳 0 回 或 0xE0 。 第二個呼叫會傳 回金鑰掃描碼

這些函式會鎖定呼叫執行緒,因此是安全線程。 如需非鎖定版本,請參閱 _getch_nolock_getwch_nolock

根據預設,此函式的全域狀態會限定于應用程式。 若要變更此行為,請參閱 CRT 中的全域狀態。

泛型文字常式對應

Tchar.h 常規 _UNICODE_MBCS 未定義 _MBCS 定義 _UNICODE 定義
_gettch _getch _getch _getwch

需求

常式 必要的標頭
_getch <conio.h>
_getwch <conio.h><wchar.h>

如需相容性詳細資訊,請參閱相容性

範例

// crt_getch.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 = _getch();
      ch = toupper( ch );
   } while( ch != 'Y' );

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

另請參閱

主控台和埠 I/O
_getche, _getwche
_cgets, _cgetws
getc, getwc
_ungetch, _ungetwch, _ungetch_nolock, _ungetwch_nolock