[英]Inspecting the LPARAM on WM_KEYDOWN - incorrect values?
我有一些简单的代码,用于在收到WM_KEYDOWN时检查LPARAM变量(发送给WNDPROC主变量)的值(位)。
但是我在那里得到一些有趣的值:在MSDN中, http : //msdn.microsoft.com/zh-cn/library/ms646280 (v=vs.85) .aspx ,它表示(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");
}
您检查这些位的代码是错误的, 并且注释中所述的位组是错误的。
例如,文档说低16位是重复计数。
你明白了
(lParam >> 0) & ((1L << 16) - 1)
在显然您的代码使用的“系统”中。
相反,您的代码的表达式不正确(lParam >> 0x01) & ((1<<15)-1))
。
然后,扫描码是接下来的8位,而不是7位。
干杯,……
您所有的蒙版计数错误,重复计数偏移量错误。 重复计数从第0位(不是第1位)开始,因此不需要进行移位,因此掩码会错过最高位。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.