kbhit() 在while循环中的用法

函数名:kbhit()
功能及返回值: 检查当前是否有键盘输入,若有则返回一个非0值,否则返回0。
用 法:int kbhit(void);
C++语言包含头文件: include <conio.h>。
C语言不需包含额外头文件。
在VC++6.0下为_kbhit()

C语言

下面的代码,如果没有键盘输入程序一直输出Hello World,直到用户按Esc结束。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<stdio.h>
#include<stdlib.h>
int  main( void )
{
char  ch;
while (ch!=27)
{
printf ( "HelloWorld\n" );
if (kbhit())
ch=getch();
}
printf ( "End!\n" );
system ( "pause" );
return  0;
}

C++语言

1
2
3
4
5
6
7
8
9
10
11
12
#include<conio.h>
#include<iostream>
using  namespace  std;
int  main()
{
while (!kbhit()) //当没有键按下
{
cout<< "无键按下" <<endl;
}
cout<< "有键按下" <<endl; //有键按下时输出这
system ( "pause" );
}
kbhit()在执行时,检测是否有按键按下,有按下返回非0值,没有按下则返回0,是非阻塞函数;

猜你喜欢

转载自blog.csdn.net/eric_e/article/details/80527231
今日推荐