[英]How do I write multiple GetAsyncKeyState with C++
My problem is where can I add this line of code: 我的问题是在哪里可以添加以下代码行:
if( GetKeyState(VK_F2) & 0x8000 )
I tried in different ways, but when I compile and run it and press the F1 or F2 key it executes all the functions. 我尝试了不同的方法,但是当我编译并运行它并按F1或F2键时,它将执行所有功能。 I want to call the F1 key functions when the key is pressed and F2 key when it is pressed.
我想在按下该键时调用F1键功能,并在按下时调用F2键。 I am using Visual Studio 2010 Express.
我正在使用Visual Studio 2010 Express。
Here is my code: 这是我的代码:
switch(uMsg)
{
case WM_INITDIALOG:
{
// do something here
break;
}
case WM_TIMER:
{
if( GetKeyState(VK_F1) & 0x8000 )
if( GetKeyState(VK_F2) & 0x8000 )
{
// do something here
}
break;
}
case WM_CLOSE:
{
// do something here
}
}
return 0;
SOLVED:) well my problem is solved after trying for sometime finally i got it the problem was that little "break;" 解决了:)好了,经过一段时间的努力,我的问题终于解决了,我明白了,那是一个很小的“中断”; statement please look at the code its working flawless.
声明请看代码,它的工作无懈可击。 Thank you so very much for your time and help appreciated
非常感谢您的宝贵时间,并感谢您的帮助
case WM_TIMER:
{
if(GetKeyState(VK_F1) & 0x8000 )
{
MessageBoxA (NULL, TEXT ("This is a call from F1 key!"), TEXT("Test F1 key"), MB_OK );
}
//break; << this was the one that was giving me the problem
if(GetKeyState(VK_F2) & 0x8000 )
{
MessageBoxA (NULL, TEXT ("This is a call from F2 key!"), TEXT("Test F2 key"), MB_OK );
}
//if(GetKeyState(VK_F3) & 0x8000 ) << i can add multi VK_ keys here
break; // << should be here
}
case WM_CLOSE:
A couple points: 几点:
WM_TIMER
s. WM_TIMER
之间按下并释放F1,很容易错过它。 GetKeyState
, as Joachim Pileborg says. GetKeyState
,如Joachim Pileborg所说。 GetAsyncKeyState
works on the state of the keyboard at the time of the call; GetAsyncKeyState
在调用时处理键盘的状态。 GetKeyState
works on the state of the keyboard when the current message was sent. GetKeyState
可处理键盘的状态。 This tends to keep things more tightly synchronized. WM_TIMER
, it probably doesn't matter so much. WM_TIMER
轮询,则可能没什么关系。 If the high-order bit is 1, the key is down;
如果高位为1,则按键按下;否则为0。 otherwise, it is up.
否则,它会上升。
If the low-order bit is 1, the key is toggled.
如果低位为1,则切换键。 A key, such as the CAPS LOCK key, is toggled if it is turned on.
如果打开了一个键(例如CAPS LOCK键),则会对其进行切换。 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.
如果低位为0,则此键处于关闭状态且不切换。切换键时,键盘上的切换键指示灯(如果有)将亮起,而当取消切换键时,其指示灯将熄灭。
Thus, to check for F1 : 因此,要检查F1 :
if( GetKeyState(VK_F1) & 0x8000 )
{
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.