How can I handle the Return key (VK_RETURN
) in a CEdit
control? The CEdit
control is parented to a CDialog
.
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- 如何让cmd.exe 执行 UNICODE 文本格式的批处理?
- 怎么把Windows开机按钮通过修改注册表指向我自己的程序
- Warning : HTML 1300 Navigation occured?
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
I encounter this problem myself. After a little experiment, a simple way existed if you just want to get the do something (after some editing etc) on the return (not specific for which editor you have focus) - I would just create a invisible default button, and let that button handle the 'return' key instead of default Ok button (of course, Ok button should be set default key to false)
By default, the Return key closes an MFC dialog. This is, because the Return key causes the
CDialog
'sOnOK()
function to be called. You can override that function in order to intercept the Return key. I got the basic idea from this article (see Method 3 at the end).First, make sure that you have added a member for the edit control to your dialog using the Class Wizard, for example:
Next, you can add the following function prototype to the header file of your dialog:
Then you can add the following implementation to the cpp file of your dialog:
Please note: If you have an OK button in your dialog which has the ID
IDOK
, then it will also callOnOK()
. If this causes any problems for you, then you have to redirect the button to another handler function. How to do this is also described in Method 3 of the article that I have mentioned above.You could also filter for the key in your dialog's PreTranslateMessage. If you get
WM_KEYDOWN
forVK_RETURN
, callGetFocus
. If focus is on your edit control, call your handling for return pressed in the edit control.Note the order of clauses in the if relies on short-circuiting to be efficient.
Make certain the Edit Control style ES_WANTRETURN is set in the dialog resource for the control
The correct answer is to handle the WM_GETDLGCODE / OnGetDlgCode message. In there you can specify that you want all keys to be handled by your class.