Share via


_kbhit

檢查主控台的鍵盤輸入。

重要

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

語法


int _kbhit( void );

傳回值

如果已按下任意按鍵,則 _kbhit 會傳回非零值。 否則,它會傳回 0。

備註

_kbhit 函式會檢查主控台的最新按鍵輸入。 如果函式傳回非零值,則緩衝區中有等候的按鍵輸入。 程式接著就可以呼叫 _getch_getche 以取得該按鍵輸入。

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

需求

常式 必要的標頭
_kbhit <conio.h>

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

程式庫

所有版本的 C 執行階段程式庫

範例

// crt_kbhit.c
// compile with: /c
/* This program loops until the user
* presses a key. If _kbhit returns nonzero, a
* keystroke is waiting in the buffer. The program
* can call _getch or _getche to get the keystroke.
*/

#include <conio.h>
#include <stdio.h>

int main( void )
{
   /* Display message until key is pressed. */
   while( !_kbhit() )
      _cputs( "Hit me!! " );

   /* Use _getch to throw key away. */
   printf( "\nKey struck was '%c'\n", _getch() );
}

範例輸出

Hit me!! Hit me!! Hit me!! Hit me!! Hit me!! Hit me!! Hit me!!
Key struck was 'q'

另請參閱

主控台和埠 I/O