控制台键盘敲击检测和解释
嘿,有人建议我应该使用 kdhit() 函数来检测控制台窗口中的按键:
“如果是 Windows,则 kbhit( ) 函数就是您想要的人。如果是 *nix,这里有一个 kbhit( ) 模拟器。 kbhit( ) 立即返回(无阻塞),并带有一个标志,表明是否有等待读取的键盘字符。您可以测试该标志以查看是否应该发出键盘读取。”
不过,我想知道读取密钥缓冲区或他所说的标志的最简单、希望是标准的方法。有什么建议吗? 谢谢!
Hey so it's been reccomended to me that i should use the kdhit() function to detect a keypress in a console window:
"If it's Windows, the kbhit( ) function is the guy you want. If it's *nix, here's a kbhit( ) emulator. kbhit( ) returns immediately (no blocking) with a flag that says that there is or is not a keyboard character waiting to be read. You can test the flag to see if you should issue a keyboard read."
However i would like to know the easiest, hopefully standard way of reading the key buffer or what he calls the flag. Any tips? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
没有标准的方法可以做到这一点,因为如上所述,无法保证您的应用程序运行在一台甚至有键盘设备的机器上,也无法保证即使有键盘设备,也无法保证用户界面功能如何执行。 em> 键盘因平台而异。
从这一点来看,无论是谁告诉您
kbhit()
是一个 Windows 函数,都误导了您。kbhit()
、getch()
及其相关函数实际上是(C 语言绑定到)的一部分MS-DOS API。它们出现在 OS/2、Win32 和其他 C/C++ 编译器的运行时库中只是为了为 MS/PC/ DR-DOS 程序。。该库将它们映射到任何存在的用于访问键盘的本机机制(如果有的话),并且通常仅以正确的方式让 TUI(而不是 GUI)应用程序访问键盘。如果您正在编写新的 TUI 应用程序,请不要使用 MS-DOS API。使用适合您的目标平台的正确的本机 API,例如 Win32 控制台 API 或 OS/2 控制台API,或 POSIX 通用终端接口(通过 ncurses 或类似的) 。
There is no standard way to do this, because, as noted, there's no guarantee that your application is running on a machine that even has a keyboard device, and how one performs user interface functions like this even when there is a keyboard varies hugely from platform to platform.
On that score, 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.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).
GetAsyncKeyState 非常有用,
看看
http://msdn.microsoft.com/en -us/library/ms646293%28v=vs.85%29.aspx
Very usefull is GetAsyncKeyState
Check it out
http://msdn.microsoft.com/en-us/library/ms646293%28v=vs.85%29.aspx
使用 _kbhit() 测试是否按下了某个键。当它使用 _getch() 来获取值时。 (注意 _getch() 可以为特殊键返回 0 或 0xE0,并为该值再次调用 _getch())
要在读取时显示字符,请使用 _getche()。
Use _kbhit() to test if a key is pressed. and when it is use _getch() to get the value. (note _getch() can return 0 or 0xE0 for special keys and call _getch() again for that value)
for displaying the characters while reading use _getche().