a_girl
a_girl

Reputation: 1205

WinAPI MSG structure determine which key was pressed

Microsoft docs contain MSG structure page, which tells:

wParam

Type: WPARAM

Additional information about the message. The exact meaning depends on the value of the message member.

lParam

Type: LPARAM

Additional information about the message. The exact meaning depends on the value of the message member.

What is this "additional meaning"? They don't even link this page with others, which explain all of the possible additional meanings.

I had some very ugly code, which determined what registered hotkey was pressed, which looks like this:

RegisterHotKey(NULL, 1, MOD_NOREPEAT, VK_F2);
...

MSG msg;
while (GetMessage(&msg, NULL, WM_HOTKEY, WM_HOTKEY) != 0)
{
    switch (htonl(msg.lParam) >> 8)
    {
    case VK_ESCAPE:
        // handle escape
    case VK_F1:
        // handle F1
    // possibly handle some more buttons
    default:
        // handle stuff
    }
}

As you can see, it is not the most elegant way to do this. Not elegant at all.

So, is there a better way (the best way) to determine which key was pressed from the MSG structure returned from GetMessage/PeekMessage?

EDIT: I understand that "meaning depends on the value of the message", give me the link to the page which maps "value of the message"->"meaning", please, instead of writing comments on how stupid am I. Much thanks.

Upvotes: 0

Views: 238

Answers (1)

Drake Wu
Drake Wu

Reputation: 7170

AFAIK, there is no unified message->meaning map page, but the document page of each individual window message. You need to know the message you want to use first, and then find the corresponding meaning in the document.

For example, the WM_HOTKEY you are using, according to the document:

wParam

The identifier of the hot key that generated the message. If the message was generated by a system-defined hot key, this parameter will be one of the following values.

lParam

The low-order word specifies the keys that were to be pressed in combination with the key specified by the high-order word to generate the WM_HOTKEY message. This word can be one or more of the following values. The high-order word specifies the virtual key code of the hot key.

is there a better way (the best way) to determine which key was pressed from the MSG structure returned from GetMessage/PeekMessage?

Determine the pressed hot key through the wParam parameter (The identifier of the hot key). The second parameter of RegisterHotKey specifies the identifier of the hot key

RegisterHotKey(NULL, 1, MOD_NOREPEAT, VK_F2);
...

    switch (msg.wParam)
    {
    case 1:
        // handle VK_F2
    default:
        // handle stuff
    }

Upvotes: 1

Related Questions