[英]WM_KEYDOWN, getting values from the lparam?
在MSDN上,對於WM_KEYDOWN定義,它說lparam的位包含:
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. 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.
( http://msdn.microsoft.com/en-us/library/ms646280%28VS.85%29.aspx )
所以我創建了一個帶有結構的聯合,如下所示:
union KeyState
{
LPARAM lparam;
struct
{
unsigned nRepeatCount : 15;
unsigned nScanCode : 8;
unsigned nExtended : 1;
unsigned nReserved : 4;
unsigned nContext : 1;
unsigned nPrev : 1;
unsigned nTrans : 1;
};
};
然后,當我在編輯框中收到wm_keydown消息時,我將其打印出來:
if (msg == WM_KEYDOWN)
{
std::tstringstream ss;
KeyState ks;
ks.lparam = lparam;
ss << "Key: " << (TCHAR)wparam << ", Val: " << (UINT)wparam << ", nRepeatCount: " << ks.nRepeatCount <<
", nScanCode: " << ks.nScanCode << ", nExtended: " << ks.nExtended << ", nReserved: " << ks.nReserved <<
", nContext: " << ks.nContext << ", nPrev: " << ks.nPrev << ", nTrans: " << ks.nTrans;
SetWindowText(hOut, ss.str().c_str());
}
我在編輯框中輸入時返回的值似乎不正確,有時nReserved甚至是1或0,nRepeatCount總是1,無論我按住鍵記錄時間還是按下隨機密鑰。
我做錯了什么嗎? 如果是這樣,從LPARAM獲得這些值的理想方法是什么?
嗯,一方面0-15是16位,而不是15位。
是的,很容易出錯(參見500的回答)。 我只想使用位圖和位移。
例如。 NRepeat = lparam&0xFFFF;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.