kbhit() (Keyboard Hit) เป็นฟังก์ชันในไลบรารี conio.h ใช้ตรวจสอบว่ามีการกดแป้นพิมพ์หรือไม่ ส่งคืนค่าที่ไม่ใช่ 0 หากมีการกด และ 0 หากไม่มี
หมายเหตุ: kbhit() ไม่ใช่ไลบรารีมาตรฐาน (non-standard) ควรหลีกเลี่ยงการใช้ในโค้ดที่ต้องการความพกพา (portable)
ตัวอย่าง
#include <stdio.h>
#include <conio.h>
int main() {
char ch;
printf("Enter keys (ESC to exit)\n");
while (1) {
if (kbhit()) {
ch = getch();
if ((int)ch == 27) // ESC key
break;
printf("You have entered: %c\n", ch);
}
}
return 0;
}
ผลลัพธ์ตัวอย่าง
Enter keys (ESC to exit) You have entered: i You have entered: t You have entered: D ...