Check if ALT key is pressed

淺唱寂寞╮ 提交于 2020-01-17 06:43:10

问题


After testing a lot I can't record if Alt key is pressed using GetAsyncKeyState in a C program. When I try this:

if (GetAsyncKeyState(VK_SHIFT))
    // do something

It works properly, but when I try this

if (GetAsyncKeyState(VK_MENU))
    // do something

It doesn't work.
So my question is "How I can record ALT?".

Thanks in advance


回答1:


I use the code below to find out the value of any key that perfectly fits in GetAsyncKeyState , I think it is 18 for ALT key .

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#if       _WIN32_WINNT < 0x0500
#undef  _WIN32_WINNT
#define _WIN32_WINNT   0x0500
#endif
#include <windows.h>
using namespace std;
int main ()
{
    char i;
    for(i=8; i<190; i++)
    {
        if(GetAsyncKeyState(i)== -32767)
        {
            cout<<int (i)<<endl;
        }
    }
    return 0;
}


来源:https://stackoverflow.com/questions/43724247/check-if-alt-key-is-pressed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!