Console Keyboard hit detection and interpretting

后端 未结 3 1781
天涯浪人
天涯浪人 2021-01-25 14:22

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

相关标签:
3条回答
  • 2021-01-25 14:27

    Very usefull is GetAsyncKeyState

    Check it out http://msdn.microsoft.com/en-us/library/ms646293%28v=vs.85%29.aspx

    0 讨论(0)
  • 2021-01-25 14:33

    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().

    0 讨论(0)
  • 2021-01-25 14:45

    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).

    0 讨论(0)
提交回复
热议问题