收到 WM_KEYDOWN 消息时 lParam 的行为是什么?
What is the behaviour of lParam when WM_KEYDOWN message recived?
我开始使用c++语言学习winapi。
我正在尝试理解 WM_KEYDOWN 消息中的 lParam
。
来自 Microsoft 文档:
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. If the keystroke is held long enough, multiple messages
are sent. However, the repeat count is not cumulative.
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. The value is always 0 for a WM_KEYDOWN message.
30: The previous key state. The value is 1 if the key is down before the
message is sent, or it is zero if the key is up.
31: The transition state. The value is always 0 for a WM_KEYDOWN message.
我是这样处理这条消息的:
case WM_KEYDOWN:
cout << ((lParam & 0b11111111111111110000000000000000) >> 16) << ", ";
cout << ((lParam & 0b00000000000000001111111100000000) >> 8) << ", ";
cout << ((lParam & 0b00000000000000000000000010000000) >> 7) << ", ";
cout << ((lParam & 0b00000000000000000000000000000100) >> 2) << ", ";
cout << ((lParam & 0b00000000000000000000000000000010) >> 1) << ", ";
cout << ((lParam & 0b00000000000000000000000000000001) >> 0) << endl;
如果我按下键盘上的 "A" 按钮,我会得到输出:
30, 0, 0, 0, 0, 1
如果我按住 "A" 按钮,我会得到多个输出:
16414, 0, 0, 0, 0, 1
16414, 0, 0, 0, 0, 1
16414, 0, 0, 0, 0, 1
如果我按其他字母按钮,我会得到相同的结果,但第一个数字不同。 (例如,对于 "B" 按钮,简单按下时我得到 48,按住时得到 49200)
如何理解?
我没有对此进行测试,但您的位顺序似乎颠倒了。根据 MS Documentation,位 0-15 是低位。
case WM_KEYDOWN:
cout << ((lParam & 0b00000000000000001111111111111111) >> 0) << ", ";
cout << ((lParam & 0b00000000111111110000000000000000) >> 16) << ", ";
cout << ((lParam & 0b00000001000000000000000000000000) >> 24) << ", ";
cout << ((lParam & 0b00100000000000000000000000000000) >> 29) << ", ";
cout << ((lParam & 0b01000000000000000000000000000000) >> 30) << ", ";
cout << ((lParam & 0b10000000000000000000000000000000) >> 31) << ", ";
我开始使用c++语言学习winapi。
我正在尝试理解 WM_KEYDOWN 消息中的 lParam
。
来自 Microsoft 文档:
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. If the keystroke is held long enough, multiple messages are sent. However, the repeat count is not cumulative.
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. The value is always 0 for a WM_KEYDOWN message.
30: The previous key state. The value is 1 if the key is down before the message is sent, or it is zero if the key is up.
31: The transition state. The value is always 0 for a WM_KEYDOWN message.
我是这样处理这条消息的:
case WM_KEYDOWN:
cout << ((lParam & 0b11111111111111110000000000000000) >> 16) << ", ";
cout << ((lParam & 0b00000000000000001111111100000000) >> 8) << ", ";
cout << ((lParam & 0b00000000000000000000000010000000) >> 7) << ", ";
cout << ((lParam & 0b00000000000000000000000000000100) >> 2) << ", ";
cout << ((lParam & 0b00000000000000000000000000000010) >> 1) << ", ";
cout << ((lParam & 0b00000000000000000000000000000001) >> 0) << endl;
如果我按下键盘上的 "A" 按钮,我会得到输出:
30, 0, 0, 0, 0, 1
如果我按住 "A" 按钮,我会得到多个输出:
16414, 0, 0, 0, 0, 1
16414, 0, 0, 0, 0, 1
16414, 0, 0, 0, 0, 1
如果我按其他字母按钮,我会得到相同的结果,但第一个数字不同。 (例如,对于 "B" 按钮,简单按下时我得到 48,按住时得到 49200)
如何理解?
我没有对此进行测试,但您的位顺序似乎颠倒了。根据 MS Documentation,位 0-15 是低位。
case WM_KEYDOWN:
cout << ((lParam & 0b00000000000000001111111111111111) >> 0) << ", ";
cout << ((lParam & 0b00000000111111110000000000000000) >> 16) << ", ";
cout << ((lParam & 0b00000001000000000000000000000000) >> 24) << ", ";
cout << ((lParam & 0b00100000000000000000000000000000) >> 29) << ", ";
cout << ((lParam & 0b01000000000000000000000000000000) >> 30) << ", ";
cout << ((lParam & 0b10000000000000000000000000000000) >> 31) << ", ";