简体   繁体   English

WM_KEYDOWN,从lparam获取值?

[英]WM_KEYDOWN, getting values from the lparam?

On MSDN, for the WM_KEYDOWN defition it says the bits of lparam contain: 在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 ) http://msdn.microsoft.com/en-us/library/ms646280%28VS.85%29.aspx

So i created a union with a struct inside like this: 所以我创建了一个带有结构的联合,如下所示:

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;
    };
};

Then when i recieve a wm_keydown message on my edit box, i print it like this: 然后,当我在编辑框中收到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());
}

The values that i get back when i'm typing in my edit box don't seem correct, sometimes the nReserved is even 1 or 0, and nRepeatCount is ALWAYS 1 no matter if i hold down the key for a logn time or just press random keys. 我在编辑框中输入时返回的值似乎不正确,有时nReserved甚至是1或0,nRepeatCount总是1,无论我按住键记录时间还是按下随机密钥。

Have i done something wrong? 我做错了什么吗? if so whats the ideal way to get these values from the LPARAM? 如果是这样,从LPARAM获得这些值的理想方法是什么?

嗯,一方面0-15是16位,而不是15位。

Yes it is easy to make mistakes (cf 500's answer). 是的,很容易出错(参见500的回答)。 I would simply use bit maps and bit shifts. 我只想使用位图和位移。

Eg. 例如。 NRepeat = lparam & 0xFFFF; NRepeat = lparam&0xFFFF;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM