user-0 提问: 键盘松开时_kbhit的值能重置吗

回复:
user: user-0
content: 引用 7 楼 赵4老师的回复:GetAsyncKeyState The GetAsyncKeyState function determines whether a key is up or down at the time the function is called, and whether the key was pressed after a previous call to GetAsyncKeyState.   SHORT GetAsyncKeyState(   int vKey   // virtual-key code );   Parameters vKey  Specifies one of 256 possible virtual-key codes. For more information, see Virtual-Key Codes.  Windows NT: You can use left- and right-distinguishing constants to specify certain keys. See the Remarks section for further information.   Return Values If the function succeeds, the return value specifies whether the key was pressed since the last call to GetAsyncKeyState, and whether the key is currently up or down. If the most significant bit is set, the key is down, and if the least significant bit is set, the key was pressed after the previous call to GetAsyncKeyState. The return value is zero if a window in another thread or process currently has the keyboard focus.   Windows 95: Windows 95 does not support the left- and right-distinguishing constants. If you call GetAsyncKeyState with these constants, the return value is zero.   Remarks The GetAsyncKeyState function works with mouse buttons. However, it checks on the state of the physical mouse buttons, not on the logical mouse buttons that the physical buttons are mapped to. For example, the call GetAsyncKeyState(VK_LBUTTON) always returns the state of the left physical mouse button, regardless of whether it is mapped to the left or right logical mouse button. You can determine the system's current mapping of physical mouse buttons to logical mouse buttons by calling   GetSystemMetrics(SM_SWAPBUTTON)    which returns TRUE if the mouse buttons have been swapped.  You can use the virtual-key code constants VK_SHIFT, VK_CONTROL, and VK_MENU as values for the vKey parameter. This gives the state of the shift, ctrl, or alt keys without distinguishing between left and right.   Windows NT: You can use the following virtual-key code constants as values for vKey to distinguish between the left and right instances of those keys.   Code Meaning  VK_LSHIFT VK_RSHIFT  VK_LCONTROL VK_RCONTROL  VK_LMENU VK_RMENU    These left- and right-distinguishing constants are only available when you call the GetKeyboardState, SetKeyboardState, GetAsyncKeyState, GetKeyState, and MapVirtualKey functions.   Windows CE: The GetAsyncKeyState function supports the left and right virtual key constants, so you can determine whether the left key or the right key was pressed. These constants are VK_LCONTROL, VK_RCONTROL, VK_LMENU, VK_RMENU, VK_LSHIFT, and VK_RSHIFT.  The least significant bit of the return value is not valid in Windows CE, and should be ignored.  GetAsyncKeyState will return the current key state even if a window in another thread or process currently has the keyboard focus.   You can also use the VK_LBUTTON virtual-key code constant to determine the state of the stylus tip, (up/down), on the touch-screen.  QuickInfo   Windows NT: Requires version 3.1 or later.   Windows: Requires Windows 95 or later.   Windows CE: Requires version 1.0 or later.   Header: Declared in winuser.h.   Import Library: Use user32.lib.  See Also Keyboard Input Overview, Keyboard Input Functions, GetKeyboardState, GetKeyState,GetSystemMetrics, MapVirtualKey, SetKeyboardState 谢谢！用GetAsyncKeyState解决了

user: user-1
content: _kbhit和_getch属于conio.h，是跳过stdin的或者说是并行于stdin旁边的，因此对stdin的操作对它们没有作用。 但是conio.h系列受限于控制台对早年DOS环境的模拟。卡顿云云是由于键盘的BIOS驱动程序或者windows底层对BIOS功能的模拟，为了防止正常打字键帽抬起不利索造成重复输入，刻意制造的至少250ms不响应，也就是说这个短期不响应和缓冲区神马的没有关系，当然也无法通过清缓冲的思路解决。  正确使用windows API才能压榨系统的功能和性能，见楼上。

user: user-2
content: GetAsyncKeyState The GetAsyncKeyState function determines whether a key is up or down at the time the function is called, and whether the key was pressed after a previous call to GetAsyncKeyState.   SHORT GetAsyncKeyState(   int vKey   // virtual-key code );   Parameters vKey  Specifies one of 256 possible virtual-key codes. For more information, see Virtual-Key Codes.  Windows NT: You can use left- and right-distinguishing constants to specify certain keys. See the Remarks section for further information.   Return Values If the function succeeds, the return value specifies whether the key was pressed since the last call to GetAsyncKeyState, and whether the key is currently up or down. If the most significant bit is set, the key is down, and if the least significant bit is set, the key was pressed after the previous call to GetAsyncKeyState. The return value is zero if a window in another thread or process currently has the keyboard focus.   Windows 95: Windows 95 does not support the left- and right-distinguishing constants. If you call GetAsyncKeyState with these constants, the return value is zero.   Remarks The GetAsyncKeyState function works with mouse buttons. However, it checks on the state of the physical mouse buttons, not on the logical mouse buttons that the physical buttons are mapped to. For example, the call GetAsyncKeyState(VK_LBUTTON) always returns the state of the left physical mouse button, regardless of whether it is mapped to the left or right logical mouse button. You can determine the system's current mapping of physical mouse buttons to logical mouse buttons by calling   GetSystemMetrics(SM_SWAPBUTTON)    which returns TRUE if the mouse buttons have been swapped.  You can use the virtual-key code constants VK_SHIFT, VK_CONTROL, and VK_MENU as values for the vKey parameter. This gives the state of the shift, ctrl, or alt keys without distinguishing between left and right.   Windows NT: You can use the following virtual-key code constants as values for vKey to distinguish between the left and right instances of those keys.   Code Meaning  VK_LSHIFT VK_RSHIFT  VK_LCONTROL VK_RCONTROL  VK_LMENU VK_RMENU    These left- and right-distinguishing constants are only available when you call the GetKeyboardState, SetKeyboardState, GetAsyncKeyState, GetKeyState, and MapVirtualKey functions.   Windows CE: The GetAsyncKeyState function supports the left and right virtual key constants, so you can determine whether the left key or the right key was pressed. These constants are VK_LCONTROL, VK_RCONTROL, VK_LMENU, VK_RMENU, VK_LSHIFT, and VK_RSHIFT.  The least significant bit of the return value is not valid in Windows CE, and should be ignored.  GetAsyncKeyState will return the current key state even if a window in another thread or process currently has the keyboard focus.   You can also use the VK_LBUTTON virtual-key code constant to determine the state of the stylus tip, (up/down), on the touch-screen.  QuickInfo   Windows NT: Requires version 3.1 or later.   Windows: Requires Windows 95 or later.   Windows CE: Requires version 1.0 or later.   Header: Declared in winuser.h.   Import Library: Use user32.lib.  See Also Keyboard Input Overview, Keyboard Input Functions, GetKeyboardState, GetKeyState,GetSystemMetrics, MapVirtualKey, SetKeyboardState

user: user-2
content: #include <conio.h> #include <windows.h> int main() {     int a,k;     while (1) {           a=_kbhit();           if (a!=0) {                 cprintf("1");                 k=getch();                 if (0==k || 0xE0==k) k=k<<8|getch();                 if (27==k) break; //按Esc键退出           } else {                 cprintf("0");           }           Sleep(20);     }     return 0; }

user: user-0
content: 想用_getch()清除键盘缓冲，这样子_kbhitd的值就重置为0了，我用GetKeyState, _kbhit下的语句就会反复执行。 我的目的是想实现静止的时候放一套动画，移动的时候放另一套动画，_kbhit不重置的话静止的时候就会播放移动的动画。现在不用_kbhit换一个思路算是解决问题了，如果能学到松键检测的话更好

user: user-1
content: 你都GetKeyState了，还要_getch干啥？不解。  另外无论是GetKeyState还是_getch都不会和stdin打交道，所以你处理stdin没有效果。

user: user-0
content: int a; while(1) {   a = _kbhit();   if (a != 0)   {     printf("1");     _getch();   }   else     printf("0"); }  加了个_getch(),实现是实现了，但是我在做游戏人物的移动，移动模块我用的是GetKeyState，但加上_getch()后，按住键盘人物会先动一下，卡一下，然后才一直移动。试着把_getch()换成rewind(stdin)或者fflush(stdin)来清除键盘缓冲，但是都没有效果，有其他清除键盘缓冲的方法吗

user: user-0
content: 这样子松开后就一直输出1，a的值没有重置，我想松开键盘之后还是输出0

user: user-1
content: int a; while(1) {   a = _kbhit();   if (a != 0)   {     printf("1");   }   else     printf("0"); }  这样不就可以看到松开的后果了？