c++ MFC中,如何响应键盘按键

abchina 2008-05-07 06:42:03
我点了 右键 event 里面找到一个 WM_KEYDOWN,在生成的代码中
 
void CTestDlg::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
CDialog::OnKeyDown(nChar, nRepCnt, nFlags);
}


都不知道怎么用!~

希望各位能提供个好办法,希望提供一个详细而简单的例子~~
...全文
1065 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
飞雁 2008-05-09
  • 打赏
  • 举报
回复
学习了不少有关MFC的知识,谢谢大家!
abchina 2008-05-09
  • 打赏
  • 举报
回复
终于可以响应啦,但是我发现如果焦点在button上就怎么按键都没用啦,用什么方法可以使消息可以从button上传到它的父窗体上呢?
genghaolove 2008-05-09
  • 打赏
  • 举报
回复
void CMainFrame::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
switch(nChar)
{
case VK_RETURN:
SetWindowText("You pressed Enter");
break;
case VK_F1:
SetWindowText("Help is not available at the moment");
break;
case VK_DELETE:
SetWindowText("Can't Delete This");
break;
default:
SetWindowText("Whatever");
}
}
abchina 2008-05-09
  • 打赏
  • 举报
回复
终于可以响应啦,但是我发现如果焦点在button上就怎么按键都没用啦,用什么方法可以使消息可以从button上传到它的父窗体上呢?
[Quote=引用 7 楼 genghaolove 的回复:]
void CMainFrame::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
switch(nChar)
{
case VK_RETURN:
SetWindowText("You pressed Enter");
break;
case VK_F1:
SetWindowText("Help is not available at the moment");
break;
case VK_DELETE:
SetWindowText("Can't Delete This");
break;
default:
SetWindowText("Whatever");
}
}
[/Quote]
花花呀123456 2008-05-08
  • 打赏
  • 举报
回复
就在这个函数里面添加你想要做的就行了。。。。。
先把消息机制看看吧!
herman~~ 2008-05-08
  • 打赏
  • 举报
回复
键盘按下了就进入这个函数了
kakaying 2008-05-08
  • 打赏
  • 举报
回复
一下内容copy自msdn
MFC函数OnKeyDown()参数说明:
nChar
Specifies the virtual key code of the given key. For a list of of standard virtual key codes, see Winuser.h

nRepCnt
Repeat count (the number of times the keystroke is repeated as a result of the user holding down the key).

nFlags
Specifies the scan code, key-transition code, previous key state, and context code, as shown in the following list:

Value Description
0–7
Scan code (OEM-dependent value).

8
Extended key, such as a function key or a key on the numeric keypad (1 if it is an extended key).

9–10
Not used.

11–12
Used internally by Windows.

13
Context code (1 if the ALT key is held down while the key is pressed; otherwise 0).

14
Previous key state (1 if the key is down before the call, 0 if the key is up).

15
Transition state (1 if the key is being released, 0 if the key is being pressed).


For a WM_KEYDOWN message, the key-transition bit (bit 15) is 0 and the context-code bit (bit 13) is 0.


WM_KEYDOWN消息参数说明:

wParam
Specifies the virtual-key code of the nonsystem key.
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.
0-15
Specifies the repeat count for the current message. The value is the number of times the keystroke is autorepeated 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 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 zero if the key is up.
31
Specifies the transition state. The value is always zero for a WM_KEYDOWN message.


知道这些信息,就可以为所欲为了
  • 打赏
  • 举报
回复
case WM_KEYDOWN:
if (wParam==VK_LEFT)//方向键左
{
rect1.left-=10;
rect1.right-=10;
InvalidateRect (hWnd,NULL,TRUE);
}
else if (wParam==VK_RIGHT)//方向键右
{
rect1.left+=10;
rect1.right+=10;
InvalidateRect (hWnd,NULL,TRUE);
}
else if (wParam==VK_UP)//方向键上
{
rect1.top-=10;
rect1.bottom-=10;
InvalidateRect (hWnd,NULL,TRUE);
}
else if (wParam==VK_DOWN)//方向键下
{
rect1.top+=10;
rect1.bottom+=10;
InvalidateRect (hWnd,NULL,TRUE);
}
else if (wParam==VK_PRIOR)//PG UP
{
rect1.top-=10;
rect1.bottom-=10;
InvalidateRect (hWnd,NULL,TRUE);
}
else if (wParam==VK_NEXT)//PG DN
{
rect1.top+=10;
rect1.bottom+=10;
InvalidateRect (hWnd,NULL,TRUE);
}
else if (wParam==VK_HOME)//HOME
{
rect1.left-=10;
rect1.right-=10;
InvalidateRect (hWnd,NULL,TRUE);
}
else if (wParam==VK_END)//END
{
rect1.left+=10;
rect1.right+=10;
InvalidateRect (hWnd,NULL,TRUE);
}
break;


以前写的一个程序.
hityct1 2008-05-08
  • 打赏
  • 举报
回复
什么意思?用重载OnChar也可。
hslinux 2008-05-08
  • 打赏
  • 举报
回复
晕,你想干吗先?

键盘按下了就进入这个函数了,你想干吗就干吗。

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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