在WM_KEYDOWN上检查LPARAM - 值不正确? [英] Inspecting the LPARAM on WM_KEYDOWN - incorrect values?

查看:253
本文介绍了在WM_KEYDOWN上检查LPARAM - 值不正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



最后,如果我按住shift键一段时间,不应该重复计数上升?当我这样做重复计数保持在零?



我的代码检查LPARAM值错误,或者我的整个消息泵? p>

  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\\\
,lParam& 0x30,lParam& 0x31,lParam& 0x32);
// printf(\\\
);
}
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
默认值:
break;
}

return DefWindowProc(hwnd,msg,wParam,lParam);
}

void outputLParam(LPARAM lParam)
{
printf(Repeat Count:%d\\\
,(lParam> 0x01)& ;((1 << 15)-1)); //打印第1个15位的值
printf(扫描代码:%d \\\
,(lParam> 0x16)&((1 << 7)-1) // print the value of the next 7 bits
printf(Extended Key:%d\\\
,lParam& 0x24); // print the value of the next bit
printf(Reserved:%d \\\
,(lParam>> 0x25)&((1< 3)-1) // print the value of the next 3 bits
printf(Context:%d \\\
,lParam& 0x29); //打印下一位的值
printf(Prev Key State:%d \\\
,lParam& 0x30); //打印下一位的值
printf(Transition Key State:%d \\\
,lParam& 0x31); //打印下一位的值
}

void outputLParamDefault(LPARAM lParam)
{
printf(LPARAM:);

for(int i = 0x01,j = 0; j <32; j ++,i ++)
{
printf(%d,lParam& i);
}

printf(\\\
);
}


解决方案

是错误的,在注释中说明的位群是错误的。





您可以通过

获得

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



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



扫描码是下一个8位,而不是7位。 / p>

干杯& hth。,


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

解决方案

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

这篇关于在WM_KEYDOWN上检查LPARAM - 值不正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆