如何在使用kbhit()和getch()时清除键缓冲区 [英] how to clear the key buffer while using kbhit() and getch()

查看:498
本文介绍了如何在使用kbhit()和getch()时清除键缓冲区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

heu,所以我使用的是上述Windows功能,幸运的是,这些功能适用于Windows 2000及更高版本,但是在控制台上进行游戏时,我遇到了一个问题:按下按键后,控制台就会通过kbhit()函数,无论是否再次按下某个键...

heu so I'm using the above stated windows functions which luckily are for windows 2000 and up, but in making a game on the console I've run into a problem: as soon as a key is pressed the console gets passed the kbhit() function no matter if a key is not pressed again...

有什么方法可以清除键盘按键缓冲区,以便在没有新按键的情况下无法通过kbhit?

如果需要下载新的lib,我想我可以...但是我希望使用Windows标准的方式!

If a new lib download is requires I guess I could... but I'm hoping for a windows standard way!

谢谢!

推荐答案

任何人告诉您kbhit()是Windows函数都会误导您. kbhit()getch()及其亲戚实际上是 MS-DOS API(C语言绑定)的一部分.它们存在于OS的运行时库中/2,Win32和其他C/C ++编译器仅是 MS/PC/DR-DOS程序的移植辅助工具.该库将它们映射到用于访问键盘的任何 native 机制中(如果有的话),并且通常仅对TUI(而不是GUI)应用程序以正确的方式访问键盘.

Whoever told you that kbhit() was a Windows function misinformed you. kbhit(), getch() and their relatives are actually part of the (C language bindings to the) MS-DOS API. Their presence in the runtime libraries for OS/2, Win32, and other C/C++ compilers is simply to be a porting aid for MS/PC/DR-DOS programs. The library maps them onto whatever native mechanism exists for accessing the keyboard, if there is one at all, and usually only in the right way for TUI, not GUI, applications to access the keyboard.

如果要编写新的TUI应用程序,请不要使用MS-DOS API.为目标平台使用正确的本机API,例如 OS/2控制台API ,或

If you're writing a new TUI application, don't use the MS-DOS API. Use the proper, native, API for the platform that you're targetting, such as the Win32 Console API or the OS/2 Console API, or the POSIX General Terminal Interface (via ncurses or some such).

当您这样做时,您将阅读他们的文档,并了解如何在进行非破坏性的窥视"之后执行破坏性的键盘输入"获取.或者实际上,如果您要这样做,如何全部刷新缓冲区.

When you do, you'll read their documentation and find out how to perform a destructive "get" of the keyboard input after doing a non-destructive "peek". Or indeed how to flush the buffer in its entirety if that is what you want to do.

这篇关于如何在使用kbhit()和getch()时清除键缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆