kbhit()函数
答案:2 悬赏:70 手机版
解决时间 2021-11-27 23:13
- 提问者网友:记得曾经
- 2021-11-27 17:04
kbhit()函数
最佳答案
- 五星知识达人网友:一袍清酒付
- 2021-11-27 18:12
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;
}
功 能及返回值: 检查当前是否有键盘输入,若有则返回一个非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;
}
全部回答
- 1楼网友:痴妹与他
- 2021-11-27 19:45
摘自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();会不能清空缓存我也不太清楚,可能与编译器有关
--------------------------------------------------------------------------------
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();会不能清空缓存我也不太清楚,可能与编译器有关
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯