I have a dialog which is a child of the mainview and the problem is when I hit the enter key on the dialog, everything closes down.
After reading several threads and a fine article, I put the following code in the dialog thinking that it would solve the problem, but it didn't do anything
In dialog box default behaviour the member function OnOK is being called if someone presses the enter button. Just override OnOK() from your base class CDialog and do nothing in it. So everything is fine though. But if you do so you need to handle the WM_ClOSE message. This can be done by adding ON_WM_CLOSE() to your message map and overriding the OnClose() member.
In dialog box default behaviour the member function OnOK is being called if someone presses the enter button.
Actually. When you press enter. The current default button (the one with the fatter border (or blue border on new XP style) is applied. This is often the OK button, but could be another.
The default button is the button that has focus or if no buttons have focus, it's the the button with the WS_DEFPUSHBUTTON style.
Only one button on the dialog can have such style. If there are more than one, the first one of those gets it. If no buttons in your template have the style, the first button (by tab order) will get it.
Easiest way to disable default enter WITHOUT removing the ability to press Enter to confirm the button with inputfocus: Add a button to your template, make it the (only) default button, and make it invisible.
How can I get the enter key to stop shutting down the app?
hi
i think handling key board events is you could try to override "PreTranslateMessage" in your Dialog, and evaluate WM_KEYDOWN/WM_KEYUP or WM_CHAR/WM_DEADCHAR messages....
If the message fits your needs (message == WM_CHAR, wParam == VK_ESCAPE) handle it, and return a nonzero value, indicating this message should NOT be dispatched, otherwise return the value of CWnd::PreTranslateMessage...
If a dialog box or one of its controls currently has the input focus, then pressing the ENTER key causes Windows to send a WM_COMMAND message with the idItem (wParam) parameter set to the ID of the default command button. If the dialog box does not have a default command button, then the idItem parameter is set to IDOK by default.
See "HOWTO: Use the ENTER Key from Edit Controls in a Dialog Box" from MSDN, Knowlegde Base (PSS ID Number: 102589, previously published under Q102589).
void CYourDialog::OnOK(void)
{
// Get the current focused window, if not OK,move to next control
// Tab settings should be proper.
CWnd* pWnd = GetFocus();
if(GetDlgItem(IDOK) != pWnd)
{
NextDlgCtrl(); // // Move to next control
return;
}
// Process the actual OnOK event
}
Do note that pressing 'Cancel' button will NOT set the focus to first control (proper tab ordered), since 'cancel' itself is button and will recieve Enter key itself.
Wel may also define macro for all dialog classes we develop:
why doesnt DiLascias code work? am using vc++2005.
as ultimately referenced below, the ...dlg.cpp was modified (the ...dlg.h file also was modified to prototypye the OnGetDefID fn) to include:
Code:
BEGIN_MESSAGE_MAP(CDisableEnterKeyDlg, CDialog)
ON_MESSAGE(DC_HASDEFID,OnGetDefID) // added
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
// CDisableEnterKeyDlg message handlers
// below function added and prototyped n ...dlg.h file
LRESULT CDisableEnterKeyDlg::OnGetDefID(WPARAM wp,LPARAM lp){
return MAKELONG(0,DC_HASDEFID);
}
when i tried to compile what was originally in DiLascia's article the compiler
rejected the added line to the BEGIN_MESSAGE_MAPs due to "error C2065: 'DM_HASDEFID' : undeclared identifier"
i changed "DM_" to "DC_" - it now compiles and runs but pressing the
Enter Key aborts the app - precisely the behavior trying to be changed.
any ideas?? project attached.
thanks - j
Last edited by jim enright; March 26th, 2007 at 11:19 AM.
Reason: improve
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.