关于MFC 计算器响应键盘消息及虚拟键码

这里说一下在基于对话框的简单计算器中如何响应键盘消息,以及普通键盘与笔记本键盘的虚拟键码。

在虚函数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:

  1. "Mappable" codes, to which Unicode characters can be assigned in the High-level editor
  2. "Non-mappable" codes (modifiers, special keys)
"Mappable" codes "Non-mappable" codes
Name Numeric value Description
VK_ABNT_C1 0xC1 Abnt C1
VK_ABNT_C2 0xC2 Abnt C2
VK_ADD 0x6B Numpad +
VK_ATTN 0xF6 Attn
VK_BACK 0x08 Backspace
VK_CANCEL 0x03 Break
VK_CLEAR 0x0C Clear
VK_CRSEL 0xF7 Cr Sel
VK_DECIMAL 0x6E Numpad .
VK_DIVIDE 0x6F Numpad /
VK_EREOF 0xF9 Er Eof
VK_ESCAPE 0x1B Esc
VK_EXECUTE 0x2B Execute
VK_EXSEL 0xF8 Ex Sel
VK_ICO_CLEAR 0xE6 IcoClr
VK_ICO_HELP 0xE3 IcoHlp
VK_KEY_0 0x30 ('0') 0
VK_KEY_1 0x31 ('1') 1
VK_KEY_2 0x32 ('2') 2
VK_KEY_3 0x33 ('3') 3
VK_KEY_4 0x34 ('4') 4
VK_KEY_5 0x35 ('5') 5
VK_KEY_6 0x36 ('6')
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值