Search code examples
c++raw-input

C++ & Raw Input - raw->data.keyboard.VKey == VK_ESCAPE is the only virtual key that works


Note that I've tested only a few, but out of those, only VK_ESCAPE will work.

What I really want is VK_LWIN and VK_RWIN, but these don't work either: VK_CONTROL, VK_LCONTROL, VK_MENU, VK_F1

I don't understand why none of those will work but VK_ESCAPE will work.

Here is the summarized code:

case WM_INPUT:
{
    LPBYTE lpb = new BYTE[dwSize];
    PRAWINPUT raw = (PRAWINPUT)lpb;
    UINT Event;

    Event = raw->data.keyboard.Message;
    keyChar = MapVirtualKey( raw->data.keyboard.VKey, MAPVK_VK_TO_CHAR);
    delete[] lpb;

    if (Event == WM_KEYUP)
    {
        if (keyChar == VK_LWIN)
        {
            system("start c:\\windows\\notepad.exe"); // For debugging, temporary
        }
    }
    break;

The intended purpose of this is to do something when the user presses the windows key, in this case that 'something' is sending a different input such as ctrl+alt+end (which is my hotkey for Launchy). Using AutoHotkey to do this isn't an option for me. I'm on Windows 10 if that makes a difference.

I code for games and 3D software normally, this is all new to me.


Solution

  • You don't need MapVirtualKey for that purpose.

    I suspect that raw->data.keyboard.VKey is a virtual code. And VK_LWIN and VK_RWIN are virtual codes. Yet they do not have character representation so MapVirtualKey makes no sense for them. ESCAPE has character code in ASCII so it works in your case.

    Thus your code should really look like:

    case WM_INPUT:
    {
        ...
        unsigned vkCode = raw->data.keyboard.VKey;
    
        if (Event == WM_KEYUP)
        {
            if (vkCode == VK_LWIN)
            {
                system("start c:\\windows\\notepad.exe"); // For debugging, temporary
            }
        }
        break;