简体   繁体   English

如何使用C ++编写多个GetAsyncKeyState

[英]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: 几点:

  1. Are you sure you want to poll like this? 您确定要这样轮询吗? Maybe you want to be responding to notifications when the user presses these keys (WM_KEYDOWN for example) instead of polling periodically? 也许您想在用户按下这些键(例如WM_KEYDOWN)时响应通知,而不是定期轮询? Your code has the problem that it can easily miss if you press & release F1 between WM_TIMER s. 您的代码有一个问题,如果您在WM_TIMER之间按下并释放F1,很容易错过它。
  2. You should probably be using 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. 这倾向于使事情保持更紧密的同步。 That said, if you're polling in WM_TIMER , it probably doesn't matter so much. 就是说,如果您在WM_TIMER轮询,则可能没什么关系。
  3. You are only checking if the return value is non-zero, which is not how this function works. 您仅在检查返回值是否为非零,这不是此函数的工作方式。 The return value is a bitmask. 返回值是一个位掩码。 From the documentation: 从文档中:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM