程序中如何区分小键盘enter与大键盘enter键

webe1234 2017-04-21 11:01:33
如题,我查了一下大小键盘上的enter键值是相同的,那么我如何让它们分别起作用?
BOOL CKeyBoardTest::PreTranslateMessage(MSG* pMsg)
{

if (pMsg->message==WM_KEYDOWN)
{
switch (pMsg->wParam)
{
case ******:
messagebox("你按下去的是大键盘enter键");
break;

case ……:
messagebox("你按下去的是小键盘enter键");
break;

return true;

}
...全文
1405 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
schlafenhamster 2017-04-25
  • 打赏
  • 举报
回复
好像 vc2012 不需要 pMsg->lParam |= 0x01000000;// bit24
「已注销」 2017-04-25
  • 打赏
  • 举报
回复
For enhanced 101- and 102-key keyboards, extended keys are the right ALT and CTRL keys on the main section of the keyboard; the INS, DEL, HOME, END, PAGE UP, PAGE DOWN, and arrow keys in the clusters to the left of the numeric keypad; and the divide (/) and ENTER keys in the numeric keypad. Other keyboards may support the extended-key bit in the lParam parameter.
「已注销」 2017-04-25
  • 打赏
  • 举报
回复
根据 #4 的代码,我找了一下 MSDN 的 WM_KEYDOWN 消息的说明,看到这些资料:
WM_KEYDOWN
wParam
	virtual-key code
lParam
	0~15    repeat code
	16~23   scan code
	24      extended key (flag)
	25~28   not used
	30      previous key state (flag)
	31      transition state (flag)
For enhanced 101- and 102-key keyboards, extended keys are the right ALT and CTRL keys on the main section of the keyboard; the INS, DEL, HOME, END, PAGE UP, PAGE DOWN, and arrow keys in the clusters to the left of the numeric keypad; and the divide (/) and ENTER keys in the numeric keypad. Other keyboards may support the extended-key bit in the lParam parameter.
不过还是不知道 #4 的代码为何在 wParam >= VK_PRIOR && wParam <= VK_HELP 时要强加一个标志位给 lParam,而非检测标志位。我觉得在收到 WM_KEYDOWN 消息时,直接检测标志位应该就能知道这个是来自哪个键:
if (wParam == VK_RETURN) {
	if (lParam & (1 << 24)) {
		// 小键盘Enter
	} else {
		// 主键盘Enter
	}
}
赵4老师 2017-04-24
  • 打赏
  • 举报
回复
引用 4 楼 schlafenhamster 的回复:

#if 1 // use lParam = scancode.
	if(pMsg->message == WM_KEYDOWN)
	{//                      0x21                        0x2F
		if (pMsg->wParam >= VK_PRIOR && pMsg->wParam <= VK_HELP)
		{// for keypad (小键盘)
			pMsg->lParam |= 0x01000000;// bit24
		}
		char str[40]={0};
		if(GetKeyNameText(pMsg->lParam, str,40) > 0)
		{
			afxDump << str << "\n";
		}
	}
#endif
	return CDialog::PreTranslateMessage(pMsg);
小键盘为 "Num Enter" 大键盘为 "Enter"
学习了!
schlafenhamster 2017-04-22
  • 打赏
  • 举报
回复

#if 1 // use lParam = scancode.
	if(pMsg->message == WM_KEYDOWN)
	{//                      0x21                        0x2F
		if (pMsg->wParam >= VK_PRIOR && pMsg->wParam <= VK_HELP)
		{// for keypad (小键盘)
			pMsg->lParam |= 0x01000000;// bit24
		}
		char str[40]={0};
		if(GetKeyNameText(pMsg->lParam, str,40) > 0)
		{
			afxDump << str << "\n";
		}
	}
#endif
	return CDialog::PreTranslateMessage(pMsg);
小键盘为 "Num Enter" 大键盘为 "Enter"
赵4老师 2017-04-21
  • 打赏
  • 举报
回复
scan code WM_KEYDOWN The WM_KEYDOWN message is posted to the window with the keyboard focus when a nonsystem key is pressed. A nonsystem key is a key that is pressed when the alt key is not pressed. WM_KEYDOWN nVirtKey = (int) wParam; // virtual-key code lKeyData = lParam; // key data Parameters nVirtKey Value of wParam. Specifies the virtual-key code of the nonsystem key. lKeyData Value of lParam. Specifies the repeat count, scan code, extended-key flag, context code, previous key-state flag, and transition-state flag, as shown in the following table. Value Description 0–15 Specifies the repeat count for the current message. The value is the number of times the keystroke is auto-repeated as a result of the user holding down the key. If the keystroke is held long enough, multiple messages are sent. However, the repeat count is not cumulative. 16–23 Specifies the scan code. The value depends on the original equipment manufacturer (OEM). 24 Specifies whether the key is an extended key, such as the right-hand alt and ctrl keys that appear on an enhanced 101- or 102-key keyboard. The value is 1 if it is an extended key; otherwise, it is 0. 25–28 Reserved; do not use. 29 Specifies the context code. The value is always 0 for a WM_KEYDOWN message. 30 Specifies the previous key state. The value is 1 if the key is down before the message is sent, or it is 0 if the key is up. 31 Specifies the transition state. The value is always 0 for a WM_KEYDOWN message.
kakabulusi 2017-04-21
  • 打赏
  • 举报
回复
从来没有遇到过回车键的区分, 不过你可以编写一段检测按键值的程序,将按键对应的所有值信息 MessageBox出来。然后你参照值来回写编写代码。

15,981

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧