这个LPARAM现在正确分开吗?

时间:2011-08-10 09:45:24

标签: c++ winapi bit-manipulation

当我收到WM_KEYDOWN事件时,我正在检查LPARAM的值。但我不确定我正在检查前16位然后接下来的8位&等等。这就是MSDN如何解释为WM_KEYDOWN组织的LPARAM信息:http://msdn.microsoft.com/en-us/library/ms646280(v=vs.85).aspx

我的位(分裂?)是否正确?:

void outputLParam( LPARAM lParam )
{
    printf("Repeat Count        : %d\n", (lParam) & ((1L<<16)-1));          // print the value of the 1st 16 bits
    printf("Scan Code           : %d\n", (lParam >> 0x16) & ((1L<<8)-1));   // print the value of the next 8 bits
    printf("Extended Key        : %d\n", lParam & 0x24);                    // print the value of the next bit
    printf("Reserved            : %d\n", (lParam >> 0x25) & ((1L<<4)-1));   // print the value of the next 4 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
}

2 个答案:

答案 0 :(得分:2)

你走了。

void outputLParam( LPARAM lParam )
{
    printf("Repeat Count        : %d\n", (lParam) & 0xFFFF);      // print the value of the 1st 16 bits
    printf("Scan Code           : %d\n", (lParam >> 16) & 0xFF);  // print the value of the next 8 bits
    printf("Extended Key        : %d\n", (lParam >> 24) & 0x1);   // print the value of the next bit
    printf("Reserved            : %d\n", (lParam >> 25) & 0xF));  // print the value of the next 4 bits
    printf("Context             : %d\n", (lParam >> 29) & 0x1);   // print the value of the next bit
    printf("Prev Key State      : %d\n", (lParam >> 30) & 0x1);   // print the value of the next bit
    printf("Transition Key State: %d\n", (lParam >> 31) & 0x1);   // print the value of the next bit
}

答案 1 :(得分:0)

在我回答in your previous question时,您应该declaring your own custom structure。它更连贯,更不容易出错。它对这种特殊情况更有意义,并充分利用了语言的结构。这里不需要任何位运算。

编辑:那就是说,安东的解决方案 是正确的。