键盘松开时_kbhit的值能重置吗

BrokenI 2021-01-26 09:09:49

int a;
while(1)
{
a = _kbhit();
if (a != 0)
{
printf("1");
}
}

按下键盘后_kbhit()下的内容会一直执行,有什么检测键盘松开的函数吗?就是松开键盘后重置a的值
...全文
414 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
BrokenI 2021-01-27
  • 打赏
  • 举报
回复
引用 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解决了
forever74 2021-01-27
  • 打赏
  • 举报
回复
_kbhit和_getch属于conio.h,是跳过stdin的或者说是并行于stdin旁边的,因此对stdin的操作对它们没有作用。 但是conio.h系列受限于控制台对早年DOS环境的模拟。卡顿云云是由于键盘的BIOS驱动程序或者windows底层对BIOS功能的模拟,为了防止正常打字键帽抬起不利索造成重复输入,刻意制造的至少250ms不响应,也就是说这个短期不响应和缓冲区神马的没有关系,当然也无法通过清缓冲的思路解决。 正确使用windows API才能压榨系统的功能和性能,见楼上。
赵4老师 2021-01-27
  • 打赏
  • 举报
回复
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
赵4老师 2021-01-27
  • 打赏
  • 举报
回复
#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;
}

BrokenI 2021-01-26
  • 打赏
  • 举报
回复
想用_getch()清除键盘缓冲,这样子_kbhitd的值就重置为0了,我用GetKeyState, _kbhit下的语句就会反复执行。 我的目的是想实现静止的时候放一套动画,移动的时候放另一套动画,_kbhit不重置的话静止的时候就会播放移动的动画。现在不用_kbhit换一个思路算是解决问题了,如果能学到松键检测的话更好
forever74 2021-01-26
  • 打赏
  • 举报
回复
你都GetKeyState了,还要_getch干啥?不解。 另外无论是GetKeyState还是_getch都不会和stdin打交道,所以你处理stdin没有效果。
BrokenI 2021-01-26
  • 打赏
  • 举报
回复

int a;
while(1)
{
  a = _kbhit();
  if (a != 0)
  {
    printf("1");
    _getch();
  }
  else
    printf("0");
}
加了个_getch(),实现是实现了,但是我在做游戏人物的移动,移动模块我用的是GetKeyState,但加上_getch()后,按住键盘人物会先动一下,卡一下,然后才一直移动。试着把_getch()换成rewind(stdin)或者fflush(stdin)来清除键盘缓冲,但是都没有效果,有其他清除键盘缓冲的方法吗
BrokenI 2021-01-26
  • 打赏
  • 举报
回复
这样子松开后就一直输出1,a的值没有重置,我想松开键盘之后还是输出0
forever74 2021-01-26
  • 打赏
  • 举报
回复

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

70,008

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧