2011-08-10 73 views
-1

我在收到WM_KEYDOWN事件時正在檢查LPARAM的值。但我不確定是否正確檢查了第一個16位然後是接下來的8位&等。這是MSDN如何解釋一個LPARAM是一個WM_KEYDOWN味精組織:http://msdn.microsoft.com/en-us/library/ms646280(v=vs.85).aspx此LPARAM現在是否正確分隔?

是我的位(分割?)正確?:

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 
} 
+0

這真的應該是你的[上一個問題]的後續/延續(http://stackoverflow.com/questions/6993957/inspecting-the-lparam-on-wm-keydown-incorrect-values)。 – Deanna

回答

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 
} 
0

正如我回答in your previous question,你確實應該declaring your own custom structure。它更加連貫且不易出錯。它對於這種特殊情況更爲合理,並且充分利用了語言的結構。這裏不需要任何位算術。

編輯:也就是說,安東的解決方案正確。

+0

爲什麼downvote?你更願意看到'(lParam)&0xFFFF'而不是'KeyInfo.nRepeatCount'? – 2011-08-11 22:49:41