WM_KEYDOWN, получать значения из lparam? - PullRequest
4 голосов
/ 08 ноября 2010

В 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?

Ответы [ 2 ]

12 голосов
/ 08 ноября 2010

Ну, с одной стороны, 0-15 это 16 бит, а не 15.

0 голосов
/ 08 ноября 2010

Да, ошибки легко совершать (ответ 500). Я бы просто использовал битовые карты и битовые сдвиги.

Например. NRepeat = lparam & 0xFFFF;

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...