共用方式為


_kbhit

檢查主控台輸入。

重要

這個應用程式開發介面不能用於 Windows 執行階段執行的應用程式。如需詳細資訊,請參閱 /ZW 不支援 CRT 函式

int _kbhit( void );

傳回值

按下按鍵時,_kbhit 會傳回非零的值。 否則,會傳回 0。

備註

_kbhit 功能檢查主控台最近的按鍵輸入。 如果函式傳回非零的值,代表有一個按鍵輸入於緩衝區等候。 程式之後可以呼叫 _getch_getche 取得按鍵輸入。

需求

常式

必要的標頭

_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