【问题标题】:Inspecting the LPARAM on WM_KEYDOWN - incorrect values?检查 WM_KEYDOWN 上的 LPARAM - 值不正确?
【发布时间】:2011-08-09 09:22:42
【问题描述】:

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

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

最后,如果我按住 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");
}

【问题讨论】:

    标签: c++ winapi bits


    【解决方案1】:

    您检查位的代码是错误的,并且 cmets 中所述的位组是错误的。

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

    你明白了

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

    在您的代码显然使用的“系统”中。

    相比之下,您的代码有不正确的表达式(lParam &gt;&gt; 0x01) &amp; ((1&lt;&lt;15)-1))

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

    干杯,

    【讨论】:

      【解决方案2】:

      你所有的掩码计数都是错误的,重复计数偏移是错误的。重复计数从第 0 位(不是第 1 位)开始,因此不需要移动,然后您的掩码会丢失最高位。

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          猜你喜欢
          • 2011-05-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-07-23
          • 1970-01-01
          相关资源
          最近更新 更多