GetKeyState函数? [英] GetKeyState function?

查看:205
本文介绍了GetKeyState函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么在按下方向箭头ON后,函数GetKeyState继续给我一个大于0的值?

  include< iostream> 
#include< windows.h>
using namespace std;

int main()
{
for(int i = 0; i <1000; ++ i)
{
if(GetKeyState VK_UP))
{
cout< UP按下< endl;
}
else
cout<< UP未按下< endl;

睡眠(150);
}

return 0;
}


解决方案 http://msdn.microsoft.com/en-gb/library/windows/desktop/ms646301.aspx =nofollow>文档:



< blockquote>

此函数返回的键状态随着线程从其消息队列中读取
键消息而改变。该状态不反映与硬件相关联的
中断级状态。使用
GetAsyncKeyState函数检索该信息。


由于您不处理消息,因此您需要调用 GetAsyncKeyState



测试正在按下的键:


$ b b

  if(GetAsyncKeyState(VK_UP)<0)
//按下键

Why after I press the directional arrow ON, the function GetKeyState continues to give me a value greater than 0?

#include <iostream>
#include <windows.h>
using namespace std;

int main()
{
    for(int i = 0; i < 1000; ++i)
    {
        if(GetKeyState(VK_UP))
        {
            cout << "UP pressed" << endl;
        }
        else
            cout << "UP not pressed" << endl;

        Sleep(150);
    }

    return 0;
}

解决方案

From the documentation:

The key status returned from this function changes as a 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.

Since you are not processing messages, you'll want to call GetAsyncKeyState instead.

Test for the key being pressed like this:

if (GetAsyncKeyState(VK_UP) < 0)
    // key is pressed

这篇关于GetKeyState函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆