VC/MFC 键盘消息的截取与响应
在MFC(Microsoft Foundation Class)对响应消息进行了封装,
可以通过重载虚函数PreTranslateMessage()对所关心的消息进行截取与响应:
以下对截取与响应键盘 Enter 消息的截取与响应
这是对话框的响应键盘 Enter 消息方式:
BOOL C**Dlg::PreTranslateMessage(MSG* pMsg)
{
}
这是View视图类响应键盘 Enter 消息的方式:
BOOL C**View::PreTranslateMessage(MSG* pMsg)
{
if (WM_KEYFIRST <= pMsg->message && pMsg->message <= WM_KEYLAST)
{
if(pMsg->wParam==VK_RETURN )
{
}
return CFormView::PreTranslateMessage(pMsg);
}
在上面的代码中,首先将pMsg->message所表示的消息同WM_KEYFIRST 和WM_KEYLAST比较,确定是键盘消息,然后通过消息参数pMsg->wParam的值来判断是否是回车键(VK_RETURN,虚拟键码可以下面相关资料中查到)。
#define VK_LBUTTON
#define VK_RBUTTON
#define VK_CANCEL
#define VK_MBUTTON
#if(_WIN32_WINNT >= 0x0500)
#define VK_XBUTTON1
#define VK_XBUTTON2
#endif
#define VK_BACK
#define VK_TAB
#define VK_CLEAR
#define VK_RETURN
#define VK_SHIFT
#define VK_CONTROL
#define VK_MENU
#define VK_PAUSE
#define VK_CAPITAL
#define VK_KANA
#define VK_HANGEUL
#define VK_HANGUL
#define VK_JUNJA
#define VK_FINAL
#define VK_HANJA
#define VK_KANJI
#define VK_ESCAPE
#define VK_CONVERT
#define VK_NONCONVERT
#define VK_ACCEPT
#define VK_MODECHANGE
#define VK_SPACE
#define VK_PRIOR
#define VK_NEXT
#define VK_END
#define VK_HOME
#define VK_LEFT
#define VK_UP
#define VK_RIGHT
#define VK_DOWN
#define VK_SELECT
#define VK_PRINT
#define VK_EXECUTE
#define VK_SNAPSHOT
#define VK_INSERT
#define VK_DELETE
#define VK_HELP
#define VK_LWIN
#define VK_RWIN
#define VK_APPS
#define VK_SLEEP
#define VK_NUMPAD0
#define VK_NUMPAD1
#define VK_NUMPAD2
#define VK_NUMPAD3
#define VK_NUMPAD4
#define VK_NUMPAD5
#define VK_NUMPAD6
#define VK_NUMPAD7
#define VK_NUMPAD8
#define VK_NUMPAD9
#define VK_MULTIPLY
#define VK_ADD
#define VK_SEPARATOR
#define VK_SUBTRACT
#define VK_DECIMAL
#define VK_DIVIDE
#define VK_F1
#define VK_F2
#define VK_F3
#define VK_F4
#define VK_F5
#define VK_F6
#define VK_F7
#define VK_F8
#define VK_F9
#define VK_F10
#define VK_F11
#define VK_F12