这里说一下在基于对话框的简单计算器中如何响应键盘消息,以及普通键盘与笔记本键盘的虚拟键码。
在虚函数PreTranslateMessage(MSG* pMsg) 中来响应键盘消息。
BOOL CCalculatorDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
if(pMsg->message ==WM_KEYDOWN)
{
switch(pMsg->wParam)
{
case 0x30: //主键盘
case VK_NUMPAD0: //小键盘
OnButton0();
break;
case 0x31:
case VK_NUMPAD1 :
OnButton1();
break;
case 0x32:
case VK_NUMPAD2 :
OnButton2();
break;
case 0x33:
case VK_NUMPAD3:
OnButton3();
break;
case 0x34:
case VK_NUMPAD4 :
OnButton4();
break;
case 0x35:
case VK_NUMPAD5 :
OnButton5();
break;
case 0x36:
case VK_NUMPAD6:
OnButton6();
break;
case 0x37:
case VK_NUMPAD7 :
OnButton7();
break;
case 0x38:
if(GetKeyState(VK_SHIFT)<0) //按键(8 *)
OnMultiply();
else
OnButton8();
break;
case VK_NUMPAD8 :
OnButton8();
break;
case 0x39:
case VK_NUMPAD9:
OnButton9();
break;
case 0xBE:
case VK_DECIMAL:
OnPoint();
break;
case 0xBB:
if(GetKeyState(VK_SHIFT)<0) //按键(+ =)
OnAdd();
else
OnEqual();
break;
case VK_ADD:
OnAdd();
break;
case 0xBD:
case VK_SUBTRACT:
OnSubtract();
break;
case VK_MULTIPLY:
OnMultiply();
break;
case 0xBF:
if(GetKeyState(VK_SHIFT)<0) //按键(?/)
OnDivide();
break;
case VK_DIVIDE:
OnDivide();
break;
case VK_RETURN:
OnEqual();
break;
case VK_ESCAPE:
case VK_BACK:
OnClear();
break;
}
return FALSE;
}
return CDialog::PreTranslateMessage(pMsg);
}
附虚拟键码表:
List of Virtual Key Codes
Following is a full list of VK codes that can be assigned to physical keys ("scan codes") in the Low-level editor.
The list is divided in two categories:
- "Mappable" codes, to which Unicode characters can be assigned in the High-level editor
- "Non-mappable" codes (modifiers, special keys)
"Mappable" codes | "Non-mappable" codes | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|