简体   繁体   English

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

[英]Inspecting the LPARAM on WM_KEYDOWN - incorrect values?

I have some simple code that inspects the LPARAM variable(sent to the main WNDPROC) values(bits) when the WM_KEYDOWN is received. 我有一些简单的代码,用于在收到WM_KEYDOWN时检查LPARAM变量(发送给WNDPROC主变量)的值(位)。

But I am getting some funny values int there: In MSDN, http://msdn.microsoft.com/en-us/library/ms646280(v=vs.85).aspx , it says that the last bit(of LPARAM) should always be 0 for a keydown message but when I output the LPARAM value its ALWAYS 1? 但是我在那里得到一些有趣的值:在MSDN中, http : //msdn.microsoft.com/zh-cn/library/ms646280 (v=vs.85) .aspx ,它表示(LPARAM的)最后一位按键消息应该始终为0,但是当我输出LPARAM值时始终为1? Also the scan code only ever alters between 5(when I press an arrow or windows key) or zero for normal letters & numbers - shouldn't they change according to the key thats pressed? 同样,扫描代码只能在5(当我按箭头或Windows键时)或零(对于正常的字母和数字)之间进行更改-它们是否应该根据所按的键进行更改?

Lastly if I hold down the shift key for a while, shouldn't the repeat count go up? 最后,如果我按住Shift键一会儿,重复计数不应该增加吗? When I do this the repeat count stays at zero? 当我这样做时,重复计数保持为零?

Is my code for inspecting LPARAM values wrong or maybe my whole message pump? 我检查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");
}

Your code for inspecting the bits is wrong, and the bitgroups stated in the comments are wrong. 您检查这些位的代码是错误的, 并且注释中所述的位组是错误的。

Eg the docs say that the lower 16 bits are the repeat count. 例如,文档说低16位是重复计数。

You get that by 你明白了

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

in the "system" that apparently your code uses. 在显然您的代码使用的“系统”中。

In contrast, your code has the incorrect expression (lParam >> 0x01) & ((1<<15)-1)) . 相反,您的代码的表达式不正确(lParam >> 0x01) & ((1<<15)-1))

The scan code is then the next 8 bits, not 7 bits. 然后,扫描码是接下来的8位,而不是7位。

Cheers & hth., 干杯,……

All your mask counts are wrong and the repeat count offset is wrong. 您所有的蒙版计数错误,重复计数偏移量错误。 The repeat count starts at bit 0 (not bit 1) so doesn't need to be shifted, and your mask then misses off the top bit. 重复计数从第0位(不是第1位)开始,因此不需要进行移位,因此掩码会错过最高位。

A question I answered earlier is relevant to your situation. 我之前回答问题与您的情况有关。 Create a custom structure instead of creating a mess with bit-shifting. 创建一个自定义结构,而不是通过位移位创建混乱的环境。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM