VC/MFC 键盘消息的截取与响应

http://hi.baidu.com/ruwind2010/blog/item/654e10d36563bc83a0ec9cbd.html

在MFC(Microsoft Foundation Class)对响应消息进行了封装,

可以通过重载虚函数PreTranslateMessage()对所关心的消息进行截取与响应:

以下对截取与响应键盘 Enter 消息的截取与响应

这是对话框的响应键盘 Enter 消息方式:
BOOL C**Dlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: 在此添加专用代码和/或调用基类
//判断是否为键盘消息
if (WM_KEYFIRST <= pMsg->message && pMsg->message <= WM_KEYLAST)
{
//判断是否按下键盘Enter键
if(pMsg->wParam==VK_RETURN)
{

//Do anything what you want to
return TRUE;
}
}
return CDialog::PreTranslateMessage(pMsg);
}

这是View视图类响应键盘 Enter 消息的方式:

BOOL C**View::PreTranslateMessage(MSG* pMsg)
{
 if (WM_KEYFIRST <= pMsg->message && pMsg->message <= WM_KEYLAST)
 {
  if(pMsg->wParam==VK_RETURN )
  {

//Do anything you want here

return TRUE;

}
 }
 return CFormView::PreTranslateMessage(pMsg);
}

  在上面的代码中,首先将pMsg->message所表示的消息同WM_KEYFIRST 和WM_KEYLAST比较,确定是键盘消息,然后通过消息参数pMsg->wParam的值来判断是否是回车键(VK_RETURN,虚拟键码可以下面相关资料中查到)。

常用的Delete键的虚拟键码为:VK_DELETE

其他相关的自己慢慢找吧,呵呵。

#define VK_LBUTTON 0x01
#define VK_RBUTTON 0x02
#define VK_CANCEL 0x03
#define VK_MBUTTON 0x04

#if(_WIN32_WINNT >= 0x0500)
#define VK_XBUTTON1 0x05
#define VK_XBUTTON2 0x06
#endif

#define VK_BACK 0x08
#define VK_TAB 0x09

#define VK_CLEAR 0x0C
#define VK_RETURN 0x0D

#define VK_SHIFT 0x10
#define VK_CONTROL 0x11
#define VK_MENU 0x12
#define VK_PAUSE 0x13
#define VK_CAPITAL 0x14

#define VK_KANA 0x15
#define VK_HANGEUL 0x15
#define VK_HANGUL 0x15
#define VK_JUNJA 0x17
#define VK_FINAL 0x18
#define VK_HANJA 0x19
#define VK_KANJI 0x19

#define VK_ESCAPE 0x1B

#define VK_CONVERT 0x1C
#define VK_NONCONVERT 0x1D
#define VK_ACCEPT 0x1E
#define VK_MODECHANGE 0x1F

#define VK_SPACE 0x20
#define VK_PRIOR 0x21
#define VK_NEXT 0x22
#define VK_END 0x23
#define VK_HOME 0x24
#define VK_LEFT 0x25
#define VK_UP 0x26
#define VK_RIGHT 0x27
#define VK_DOWN 0x28
#define VK_SELECT 0x29
#define VK_PRINT 0x2A
#define VK_EXECUTE 0x2B
#define VK_SNAPSHOT 0x2C
#define VK_INSERT 0x2D
#define VK_DELETE 0x2E
#define VK_HELP 0x2F

#define VK_LWIN 0x5B
#define VK_RWIN 0x5C
#define VK_APPS 0x5D

#define VK_SLEEP 0x5F

#define VK_NUMPAD0 0x60
#define VK_NUMPAD1 0x61
#define VK_NUMPAD2 0x62
#define VK_NUMPAD3 0x63
#define VK_NUMPAD4 0x64
#define VK_NUMPAD5 0x65
#define VK_NUMPAD6 0x66
#define VK_NUMPAD7 0x67
#define VK_NUMPAD8 0x68
#define VK_NUMPAD9 0x69
#define VK_MULTIPLY 0x6A
#define VK_ADD 0x6B
#define VK_SEPARATOR 0x6C
#define VK_SUBTRACT 0x6D
#define VK_DECIMAL 0x6E
#define VK_DIVIDE 0x6F
#define VK_F1 0x70
#define VK_F2 0x71
#define VK_F3 0x72
#define VK_F4 0x73
#define VK_F5 0x74
#define VK_F6 0x75
#define VK_F7 0x76
#define VK_F8 0x77
#define VK_F9 0x78
#define VK_F10 0x79
#define VK_F11 0x7A
#define VK_F12

阅读更多
个人分类: C++&&MFC
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭