关于WM_KEYDOWN的消息中传递的是该键的虚拟扫描码,不是ASCII码。它传递了键被按下时的特征,细节的描述如下:

wParamThe virtual-key code of the nonsystem key. See Virtual-Key Codes.

lParamThe repeat count, scan code, extended-key flag, context code, previous key-state flag, and transition-state flag, as shown following.例如,我们实现一个打印输入的virtual code的程序,只要在前面的程序中,填入:

 1     case WM_KEYDOWN:
 2         {
 3             char virtual_code = wparam;
 4             int key_state = lparam;
 5     
 6          // get a graphics context
 7          hdc = GetDC(hwnd);
 8 
 9          // set the foreground color to green
10          SetTextColor(hdc, RGB(0,255,0));
11          
12          // set the background color to black
13          SetBkColor(hdc, RGB(0,0,0));
14          
15          // set the transparency mode to OPAQUE
16          SetBkMode(hdc, OPAQUE);
17 
18          // print the virtual code and key state
19          sprintf(buffer,"WM_KEYDOWN: Character = 0X%X   ",virtual_code);
20          TextOut(hdc, 0,32, buffer, strlen(buffer));
21 
22          // release the dc back
23          ReleaseDC(hwnd, hdc);        
24         
25         }break;

 

最后要介绍的是键盘状态函数:GetAsyncKeyState(). 函数的原型如下:

short GetAsyncKeyState(int virtual_key); 返回值的最高位为1表示该键被按下,否则该键被松开。一般我们可以采用简单的宏代替来完成测试。

View Code
#define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEYUP(vk_code)   ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)

 

posted on 2013-02-12 18:36  GreenLight  阅读(463)  评论(0编辑  收藏  举报