在WM_KEYDOWN上检查LPARAM - 值不正确?

时间:2011-08-09 09:22:43

标签: c++ winapi bits

我有一些简单的代码,可以在收到WM_KEYDOWN时检查LPARAM变量(发送到主WNDPROC)的值(位)。

但我在那里得到了一些有趣的值:在MSDN中,http://msdn.microsoft.com/en-us/library/ms646280(v=vs.85).aspx,它表示对于keydown消息,最后一位(LPARAM)应始终为0但是当我输出LPARAM值总是1?此外,扫描码只能在5(当我按箭头或窗口键)之间改变或在正常字母和0之间改变。数字 - 它们不应该根据按下的键改变吗?

最后,如果我按住shift键一段时间,重复计数不应该上升吗?当我这样做时,重复计数保持为零?

检查LPARAM值的代码是错误的还是我的整个消息泵?

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{

    switch(msg)
    {
        case WM_KEYDOWN:
        {
            outputLParam( lParam );
            outputLParamDefault( lParam );
            //printf("A: %d, %d, %d\n", lParam & 0x30, lParam & 0x31, lParam & 0x32 );
            //printf("\n");
        }
        break;
        case WM_CLOSE:
            DestroyWindow(hwnd);
        break;
        case WM_DESTROY:
            PostQuitMessage(0);
        break;
        default: 
        break;
    }

    return DefWindowProc(hwnd, msg, wParam, lParam);
}

void outputLParam( LPARAM lParam )
{
    printf("Repeat Count        : %d\n", (lParam >> 0x01) & ((1<<15)-1));  // print the value of the 1st 15 bits
    printf("Scan Code           : %d\n", (lParam >> 0x16) & ((1<<7)-1));   // print the value of the next 7 bits
    printf("Extended Key        : %d\n", lParam & 0x24);                   // print the value of the next bit
    printf("Reserved            : %d\n", (lParam >> 0x25) & ((1<<3)-1));   // print the value of the next 3 bits
    printf("Context             : %d\n", lParam & 0x29);                   // print the value of the next bit
    printf("Prev Key State      : %d\n", lParam & 0x30);                   // print the value of the next bit
    printf("Transition Key State: %d\n", lParam & 0x31);                   // print the value of the next bit
}

void outputLParamDefault( LPARAM lParam )
{
    printf("LPARAM: ");

    for ( int i=0x01, j=0; j<32; j++, i++)
    {
        printf("%d", lParam & i);
    } 

    printf("\n");
}

3 个答案:

答案 0 :(得分:1)

您检查位的代码是错误的,注释中所述的位组是错误的。

E.g。文档说低16位是重复计数。

你可以通过

获得
(lParam >> 0) & ((1L << 16) - 1)
显然是你的代码使用的“系统”中的

相反,您的代码的表达式(lParam >> 0x01) & ((1<<15)-1))不正确。

扫描码是接下来的8位,而不是7位。

干杯&amp;第h。,

答案 1 :(得分:0)

所有掩码计数都是错误的,重复计数偏移是错误的。重复计数从第0位开始(不是第1位),所以不需要移位,然后你的掩码错过了最高位。

答案 2 :(得分:0)

question I answered earlier与您的情况相关。创建一个自定义结构,而不是创建一个带位移的混乱。

相关问题