关于GetKeyState函数,实践怎么和书上所说不一致

ResearchWorld 2012-11-16 08:38:37
为什么书上说GetKeyState函数,可以获得按键的当前状态。可我按书上写了如下代码:
代码所示:
case WM_KEYDOWN:
if (wParam==VK_TAB)
{
if(GetAsyncKeyState(VK_SHIFT)==-1)
{
MessageBox(hwnd,TEXT("按下了SHIFT键"),"",NULL);
}
}
为什么编译运行此段代码后,同时按下了SHIFT和TAB键,程序没反应
...全文
362 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
wingzero520 2012-12-25
  • 打赏
  • 举报
回复
if(GetAsyncKeyState(VK_SHIFT)==-1) 改成 if(GetAsyncKeyState(VK_SHIFT) & 0x8000)
ForestDB 2012-12-25
  • 打赏
  • 举报
回复
好奇哪本书?
jiangandlijie1 2012-12-25
  • 打赏
  • 举报
回复
应该用GetKeyState吧可能,GetAsyncKeyState是用在消息处理程序外面的。
ResearchWorld 2012-11-17
  • 打赏
  • 举报
回复
引用 4 楼 anhuizhuanjiao 的回复:
引用 3 楼 CX594 的回复: 引用 1 楼 anhuizhuanjiao 的回复:只有在呼叫GetKeyState 期间处理讯息时,而左键已经按下,才会报告键已经按下的讯息, 我感觉当是当你按下SHIFT键后 if(GetAsyncKeyState(VK_SHIFT)==-1)已经执行完了而你的TAB键还没按 同时按下的 怎么会同时按下,CPU处理一条指令的速度是几纳秒,你按两个键……
问题是 几种情况 ,我肯定都试过的 ,例如先按的 tab 后shift ,或一直按着shift 然后 tab 都无反应
转角天边 2012-11-16
  • 打赏
  • 举报
回复
引用 3 楼 CX594 的回复:
引用 1 楼 anhuizhuanjiao 的回复:只有在呼叫GetKeyState 期间处理讯息时,而左键已经按下,才会报告键已经按下的讯息, 我感觉当是当你按下SHIFT键后 if(GetAsyncKeyState(VK_SHIFT)==-1)已经执行完了而你的TAB键还没按 同时按下的
怎么会同时按下,CPU处理一条指令的速度是几纳秒,你按两个键的之间的间隔是多少
ResearchWorld 2012-11-16
  • 打赏
  • 举报
回复
引用 1 楼 anhuizhuanjiao 的回复:
只有在呼叫GetKeyState 期间处理讯息时,而左键已经按下,才会报告键已经按下的讯息, 我感觉当是当你按下SHIFT键后 if(GetAsyncKeyState(VK_SHIFT)==-1)已经执行完了而你的TAB键还没按
同时按下的
JiMoKuangXiangQu 2012-11-16
  • 打赏
  • 举报
回复
查查msdn: GetKeyStateSee Also GetAsyncKeyState | MapVirtualKey Requirements OS Versions: Windows CE 1.0 and later. Header: Winuser.h. Link Library: Kbdui.lib. This function retrieves the status of the specified virtual key. The status specifies whether the key is up, down, or toggled on or off — alternating each time the key is pressed. SHORT GetKeyState( int nVirtKey ); Parameters nVirtKey [in] Specifies a virtual key. If the desired virtual key is a letter or digit (A through Z, a through z, or 0 through 9), nVirtKey must be set to the ASCII value of that character. For other keys, it must be a virtual-key code. If a non-English keyboard layout is used, virtual keys with values in the range ASCII A through Z and 0 through 9 are used to specify most of the character keys. For example, for the German keyboard layout, the virtual key of value ASCII O (0x4F) refers to the "o" key, whereas VK_OEM_1 refers to the "o with umlaut" key. Return Values The return value specifies the status of the specified virtual key, as follows: If the high-order bit is 1, the key is down; otherwise, it is up. If the low-order bit is 1, the key is toggled. A key, such as the CAPS LOCK key, is toggled if it is turned on. The key is off and untoggled if the low-order bit is 0. A toggle key's indicator light (if any) on the keyboard will be on when the key is toggled, and off when the key is untoggled. Remarks The GetKeyState function can be used to check the down state of the following virtual keys: VK_CONTROL VK_RCONTROL VK_LCONTROL VK_RMENU VK_LSHIFT VK_RSHIFT VK_LMENU VK_SHIFT VK_MENU For Windows CE 1.0 through 2.01, GetKeyState is used to check the toggled state of the VK_CAPITAL virtual key only. For Windows CE 2.10 and later, the toggle state of the VK_NUMLOCK virtual key can also be checked. The key status returned from this function changes as a specified thread reads key messages from its message queue. The status does not reflect the interrupt-level state associated with the hardware. Use the GetAsyncKeyState function to retrieve that information. An application calls GetKeyState in response to a keyboard-input message. This function retrieves the state of the key when the input message was generated. An application can use the virtual-key code constants VK_SHIFT, VK_CONTROL, and VK_MENU as values for the nVirtKey parameter. This gives the status of the SHIFT, CTRL, or ALT keys without distinguishing between left and right. An application can also use the following virtual-key code constants as values for nVirtKey to distinguish between the left and right instances of those keys: VK_LSHIFT VK_RSHIFT VK_LCONTROL VK_RCONTROL VK_LMENU VK_RMENU These left- and right-distinguishing constants are available to an application only through the GetAsyncKeyState, GetKeyState, and MapVirtualKey functions. Requirements OS Versions: Windows CE 1.0 and later. Header: Winuser.h. Link Library: Kbdui.lib.
转角天边 2012-11-16
  • 打赏
  • 举报
回复
只有在呼叫GetKeyState 期间处理讯息时,而左键已经按下,才会报告键已经按下的讯息, 我感觉当是当你按下SHIFT键后 if(GetAsyncKeyState(VK_SHIFT)==-1)已经执行完了而你的TAB键还没按

69,371

社区成员

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

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