检查WM_KEYDOWN上的LPARAM -不正确的值

Inspecting the LPARAM on WM_KEYDOWN - incorrect values?

本文关键字:不正确 LPARAM 上的 WM KEYDOWN 检查      更新时间:2023-10-16

我有一些简单的代码,检查LPARAM变量(发送到主WNDPROC)值(位)当WM_KEYDOWN被接收。

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

最后,如果我按住shift键一段时间,重复计数不应该上升吗?当我这样做的时候,重复计数一直是0吗?

我检查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, %dn", 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        : %dn", (lParam >> 0x01) & ((1<<15)-1));  // print the value of the 1st 15 bits
    printf("Scan Code           : %dn", (lParam >> 0x16) & ((1<<7)-1));   // print the value of the next 7 bits
    printf("Extended Key        : %dn", lParam & 0x24);                   // print the value of the next bit
    printf("Reserved            : %dn", (lParam >> 0x25) & ((1<<3)-1));   // print the value of the next 3 bits
    printf("Context             : %dn", lParam & 0x29);                   // print the value of the next bit
    printf("Prev Key State      : %dn", lParam & 0x30);                   // print the value of the next bit
    printf("Transition Key State: %dn", 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");
}

您检查位的代码是错误的,注释中声明的bitgroups是错误的。

。文档中说低16位是重复计数。

通过

得到
(lParam >> 0) & ((1L << 16) - 1)

在你的代码使用的"system"中。

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

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

干杯,hth。

所有的掩码计数都是错误的,重复计数偏移量是错误的。重复计数从第0位开始(而不是第1位),因此不需要移位,然后您的掩码会错过顶部位。

我刚才回答的一个问题与你的情况有关。创建一个自定义的结构,而不是创建一个混乱的位移位