在执行时,检测是否有按键按下,有按下返回键值
没有按下返回0是非阻塞函数
例子如下:#include
<conio.h>
int
main(void)
{
cprintf("Press
any
key
to
continue:")
while
(!kbhit())
/*
do
nothing
*/
//用返回值来判断
逻辑关系
cprintf("\r\nA
key
was
pressed...\r\n")
return
0
}
函数名:kbhit()(VC++6.0下为_kbhit())功
能及返回值:
检查当前是否有键盘输入,若有则返回一个非0值,否则返回0
用
法:int
kbhit(void)
包含头文件:
include
#include
int
main(void)
{
cprintf("Press
any
key
to
continue:")
while
(!kbhit())
/*
do
nothing
*/
cprintf("\r\nA
key
was
pressed...\r\n")
return
0
}
下面的代码,如果没有键盘输入程序一直输出Hello
World,直到用户按Esc结束
#include
#include
int
main(
void
)
{
char
ch
while(
!kbhit()
)
{
cprintf("Hello
World\n")
if(
kbhit()
)
{
ch
=
getch()
if(
27
==
ch
)
break
}
}
cprintf("End!\n")
system("pause")
return
0
}