kbhit()函数
/*#include<iostream>usingnamespacestd;#include<conio.h>voidmain(){while(true){if(kbhi...
/*
#include <iostream>
using namespace std;
#include <conio.h>
void main()
{
while(true)
{
if(kbhit())
{
cout<<1;
cin.sync();
if(kbhit())
{
cout<<2<<endl;//会输出
}
system("pause");
}
}
}*/
/*
#include <iostream>
using namespace std;
#include <conio.h>
void main()
{
while(true)
{
if(kbhit())
{
cout<<1;
char c=getch();
if(kbhit())
{
cout<<2<<endl;//不会输出
}
system("pause");
}
}
}*/
/*
#include <iostream>
using namespace std;
#include <conio.h>
void main()
{
while(true)
{
if(kbhit())
{
cout<<1;
if(kbhit())
{
cout<<2<<endl;//2会输出
}
system("pause");
}
}
}*/
以上是三个程序
我的问题是 kbhit()检查键盘有无按下,第一个程序当按下键盘上的键时,第一个kbhit()为真,可以输出1,但是为什么第二个kbhit()也为真
为什么第二个程序的第二个kbhit()为假
请解惑 展开
#include <iostream>
using namespace std;
#include <conio.h>
void main()
{
while(true)
{
if(kbhit())
{
cout<<1;
cin.sync();
if(kbhit())
{
cout<<2<<endl;//会输出
}
system("pause");
}
}
}*/
/*
#include <iostream>
using namespace std;
#include <conio.h>
void main()
{
while(true)
{
if(kbhit())
{
cout<<1;
char c=getch();
if(kbhit())
{
cout<<2<<endl;//不会输出
}
system("pause");
}
}
}*/
/*
#include <iostream>
using namespace std;
#include <conio.h>
void main()
{
while(true)
{
if(kbhit())
{
cout<<1;
if(kbhit())
{
cout<<2<<endl;//2会输出
}
system("pause");
}
}
}*/
以上是三个程序
我的问题是 kbhit()检查键盘有无按下,第一个程序当按下键盘上的键时,第一个kbhit()为真,可以输出1,但是为什么第二个kbhit()也为真
为什么第二个程序的第二个kbhit()为假
请解惑 展开
2个回答
展开全部
kbhit()(VC++6.0下为_kbhit())
功 能及返回值: 检查当前是否有键盘输入,若有则返回一个非0值,否则返回0
用 法:int kbhit(void);
包含头文件: include <conio.h>
程序示例:
C语言
下面的代码,如果没有键盘输入程序一直输出Hello World,直到用户按Esc结束。
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
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;
}
功 能及返回值: 检查当前是否有键盘输入,若有则返回一个非0值,否则返回0
用 法:int kbhit(void);
包含头文件: include <conio.h>
程序示例:
C语言
下面的代码,如果没有键盘输入程序一直输出Hello World,直到用户按Esc结束。
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
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
/*
* Compiler options needed: None
*/
#include <conio.h>
void main(void)
{
while (!kbhit())
; // Waits for keystroke
while (kbhit())
getch(); // Empties buffer
while (!kbhit())
; // Waits for keystroke
}
kbhit()不会清空缓冲区内容,一旦有键盘事件,必须使用getch()清空缓存,至于为什么cin.sync();会不能清空缓存我也不太清楚,可能与编译器有关
--------------------------------------------------------------------------------
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
/*
* Compiler options needed: None
*/
#include <conio.h>
void main(void)
{
while (!kbhit())
; // Waits for keystroke
while (kbhit())
getch(); // Empties buffer
while (!kbhit())
; // Waits for keystroke
}
kbhit()不会清空缓冲区内容,一旦有键盘事件,必须使用getch()清空缓存,至于为什么cin.sync();会不能清空缓存我也不太清楚,可能与编译器有关
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |