The meaning of LPARAM
and WPARAM
vary for the specific message being processed. This is why the documentation for PostMessage
cannot get too specific for these parameters, only stating:
Additional message-specific information.
On both. To know exactly what they mean for each message, you need to look at the documentation for that message.
In the case of the messages you're asking about, WM_KEYUP
and WM_KEYDOWN
, the value of LPARAM
indicates:
The repeat count, scan code, extended-key flag, context code, previous key-state flag, and transition-state flag, as shown in the following table. (Source #1, #2)
Bits Meaning
0-15 The repeat count for the current message. The value is the number of times the keystroke is autorepeated as a result of the user holding down the key.
16-23 The scan code. The value depends on the OEM.
24 Indicates whether the key is an extended key, such as the right-hand ALT and CTRL keys that appear on an enhanced 101- or 102-key keyboard. The value is 1 if it is an extended key; otherwise, it is 0.
25-28 Reserved; do not use.
29 The context code.
30 The previous key state.
31 The transition state.
Let's look at the bits for your WM_KEYDOWN
LPARAM
there:
0x002C0001
0b0000000000101100000000000001
The bits that are set are 21, 19, 18, and 0. That tells us that:
Repeat count is 1
The remaining bits are the scan code for z
, which is clearly 0b00101100 or 0x2C.
The WM_KEYUP
message has the LPARAM
value 0xC02C0001, which only differs at the most significant nybble, giving us:
0b1100000000101100000000000001
So, the only difference here is that the previous state and transition state bits are both 1, which is guaranteed for a WM_KEYUP
message anyway.
With regard to your other question:
Is it possible to create two functions, say, CreateLPARAM_KeyDown() and CreateLPARAM_KeyUp() where you just pass the scan code?
Sure. Look at MapVirtualKey
to determine how to get the scan code from a key code, and use bit operations to construct a 32-bit LPARAM
from that and everything else you know from the table above about the bits that must be set for these messages. You will need to use bit shifting and other bit operations to accomplish this, since the scan code is a single 8-bit byte stored as part of a 32-bit LPARAM
.