通常我们在 Console 的程序中需要等待按键之类的,通常使用的都是:conio.h中的getch() 或者 stdio.h中的getchar()【参考1】。美中不足的是,这两个函数都会一直在那里等待按键,不会去做其他的事情。如果我们需要实现类似Pascal语言中 Keypress 函数,“检查一下是否有按键,没有的话我继续做其他事情”的功能,就需要其他函数了。
那就是 conio.h中的kbhit()
#include "stdafx.h"
#include<conio.h>
#include<stdio.h>
#include "windows.h"
using namespace System;
int main(array<System::String ^> ^args)
{
int i=0;
while (!kbhit())
{
printf("lab-z.com [%d]\n",i++);
Sleep(200);
}
system("PAUSE");
return 0;
}
运行结果
1.http://baike.baidu.com/view/7942479.htm 阻塞函数
