获取按键或按键的键代码

时间:2019-12-08 17:02:16

标签: c++

我尝试使用简单的控制台应用程序c ++获取按下按钮或鼠标按钮的虚拟键代码,其键正常工作,我可以获取一个值,但是idk如何获取鼠标按钮值,例如我按下Rbutton“ 0x002”或LBtton“ 0x001”。是的,我已经尝试找到有关的信息,但没有什么比我需要的更近

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

bool getconchar(KEY_EVENT_RECORD& krec)
{
    DWORD cc;
    INPUT_RECORD irec;
    HANDLE h = GetStdHandle(STD_INPUT_HANDLE);

    if (h == NULL)
    {
        return false; // console not found
    }

    for (; ; )
    {
        ReadConsoleInput(h, &irec, 1, &cc);
        if (irec.EventType == KEY_EVENT
            && ((KEY_EVENT_RECORD&)irec.Event).bKeyDown
            )//&& ! ((KEY_EVENT_RECORD&)irec.Event).wRepeatCount )
        {
            krec = (KEY_EVENT_RECORD&)irec.Event;
            return true;
        }
    }
    return false; //future ????
}

int main()
{
    KEY_EVENT_RECORD key;

    WORD Aimkey = NULL;

    for (; ; )
    {

        getconchar(key);
        std::cout << "key: " << key.uChar.AsciiChar
            << " code: " << "0x" << std::hex << key.wVirtualKeyCode << std::endl;

    }
}

输出:

key: m code: 0x4d
key: a code: 0x41
key: s code: 0x53
key: d code: 0x44
mouse ???

0 个答案:

没有答案