Dialog程序本来没有直接响应键盘的事件,如果你用OnKeyDown会发现不起作用。
那么你想按下“左键”、“W键”、“9键盘”应该怎么响应呢?
重载基类的BOOL PreTranslateMessage(MSG* pMsg)函数
头文件声明:
BOOL PreTranslateMessage(MSG* pMsg);
源文件定义:
BOOL CmfcDlg::PreTranslateMessage(MSG* pMsg)
{
if (pMsg->message == WM_KEYDOWN)
{
switch (pMsg->wParam)
{
case VK_ESCAPE://屏蔽ESCAPE键
return TRUE;
//VK_A - VK_Z are the same as ASCII ‘A’ - ‘Z’ (0x41 - 0x5A) 不区分大小写
case ‘A’:
AfxMessageBox(_T(“A key down”));return TRUE;
case VK_RIGHT:
AfxMessageBox(_T(“-> key down”));return TRUE;
case ‘0’:
AfxMessageBox(_T(“0 key down”));return TRUE;
default:
return TRUE;
}
}
return CDialogEx::PreTranslateMessage(pMsg);
}
原文地址:http://blog.csdn.net/calmreason/article/details/43763249