【问题标题】:Check if ALT key is pressed检查是否按下了 ALT 键
【发布时间】:2017-05-01 18:22:34
【问题描述】:

经过大量测试后,如果在 C 程序中使用 GetAsyncKeyState 按下 Alt 键,我无法记录。 当我尝试这个时:

if (GetAsyncKeyState(VK_SHIFT))
    // do something

它工作正常,但是当我尝试这个时

if (GetAsyncKeyState(VK_MENU))
    // do something

它不起作用。
所以我的问题是“我如何记录 ALT?”。

提前致谢

【问题讨论】:

  • 什么不起作用?程序崩溃?程序不建立? 'if' 之后的部分永远不会被调用?你在测试什么?单独按 Alt 键或与另一个字符组合按?
  • 您能解释一下为什么“roy”是最佳答案吗?阅读下面的微软网页docs.microsoft.com/en-us/windows/win32/inputdev/…你可以立即看到 VK_MENU = 0x12 = 18 !?

标签: c windows keyboard-shortcuts


【解决方案1】:

我使用下面的代码找出完全适合 GetAsyncKeyState 的任何键的值,我认为 ALT 键的值是 18。

#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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-05
    • 2012-11-12
    • 2014-01-15
    • 2023-03-03
    • 2012-11-16
    • 2016-03-13
    • 1970-01-01
    相关资源
    最近更新 更多