永发信息网

kbhit()函数

答案:2  悬赏:70  手机版
解决时间 2021-11-27 23:13
kbhit()函数
最佳答案
kbhit()(VC++6.0下为_kbhit())
  功 能及返回值: 检查当前是否有键盘输入,若有则返回一个非0值,否则返回0
  用 法:int kbhit(void);
  包含头文件: include
  程序示例:
  C语言
  下面的代码,如果没有键盘输入程序一直输出Hello World,直到用户按Esc结束。
  #include
  #include
  #include
  int main(void)
  {
  char ch;
  while(1)
  {
  printf("HelloWorld\n");
  if(kbhit())
  {
  ch=getch();
  if(27==ch)
  break;
  }
  }
  printf("End!\n");
  system("pause");
  return 0;
  }
全部回答
摘自MSDN:
--------------------------------------------------------------------------------

kbhit() Does Not Take Character out of Buffer

Article Last Modified on 03-26-2002

The information in this article applies to:

The C Run-Time (CRT)
Microsoft C for MS-DOS
Microsoft C for OS/2
Microsoft C/C++ for MS-DOS
Microsoft Visual C++ for Windows, 16-bit edition 1.0, 1.5
Microsoft Visual C++, 32-bit Editions 1.0, 2.0, 4.0, 5.0, 6.0
Microsoft Visual C++ .NET (2002)

--------------------------------------------------------------------------------
NOTE: Microsoft Visual C++ NET (2002) supported both the managed code model that is provided by the .NET Framework and the unmanaged native Windows code model. The information in this article applies to unmanaged Visual C++ code only.

Summary
When an application tests the value returned by the kbhit() function, the application must explicitly remove the character from the keyboard buffer after entering the keystroke. Otherwise, kbhit() continues to return TRUE.

The following code example demonstrates the situation:

Sample Code


#include

void main(void)
{
while (!kbhit())
; // Waits for keystroke
while (kbhit())
getch(); // Empties buffer
while (!kbhit())
; // Waits for keystroke
}

kbhit()不会清空缓冲区内容,一旦有键盘事件,必须使用getch()清空缓存,至于为什么cin.sync();会不能清空缓存我也不太清楚,可能与编译器有关
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
鲫鱼春夏秋冬分别爱吃什么味的鱼食
如何游览台山市斗山镇的浮月村和端芬镇的汀江
W10屏幕亮度怎么调节
索尼降噪耳机nc020价值多少钱音质如何
小红和小兰共有48朵花,小红给小兰5朵,小兰
若ab≠0,则a/|a|+b/|b|的值不可能
我国北方的农民在什么时候开始春耕
为啥潮辣火锅被人黑了?咋回事啊?
河里垂钓水深3一5米主钓鲫鱼监钓鲤鱼,商品饵
带蒸汽的淋浴房好不好用
车票3月6号0点50分开车,请问我该5号晚上乘车
goland格式化代码会将注释换行怎么改
一个数用科学记数法表示为3.05x10的n次方加2(
奶黄瓜怎么做
初三化学的,做所有题,c
推荐资讯
成语:四什么剪刀
44+44=88/45
落户上海的条件是什么?
探究下面的材料,说说你能获得什么启示。(5分
在万三传媒上批量发文有优惠吗?
UTF-8是按拼音顺序进行汉字编码的吗
国道233淄博段何时通车?
福建柚子原产地什么时候可以上市呢?
搬水泥的高大上叫法
残疾军人坐地铁免费吗
娄娄花炖肉还放不放姜
.The girl Susan, who often help other peo
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?