Inspecting the LPARAM on WM_KEYDOWN - incorrect values?

自作多情 提交于 2019-12-12 01:48:41

问题


I have some simple code that inspects the LPARAM variable(sent to the main WNDPROC) values(bits) when the WM_KEYDOWN is received.

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? 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?

Lastly if I hold down the shift key for a while, shouldn't the repeat count go up? When I do this the repeat count stays at zero?

Is my code for inspecting LPARAM values wrong or maybe my whole message pump?

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");
}

回答1:


Your code for inspecting the bits is wrong, and the bitgroups stated in the comments are wrong.

E.g. the docs say that the lower 16 bits are the repeat count.

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)).

The scan code is then the next 8 bits, not 7 bits.

Cheers & hth.,




回答2:


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.




回答3:


A question I answered earlier is relevant to your situation. Create a custom structure instead of creating a mess with bit-shifting.



来源:https://stackoverflow.com/questions/6993957/inspecting-the-lparam-on-wm-keydown-incorrect-values

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!