CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    May 1999
    Posts
    117

    Problem with enter key

    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 MyDlg.h:
    Code:
    	// Generated message map functions
    	//{{AFX_MSG(CMyDlg)
    	virtual BOOL OnInitDialog();
    	afx_msg void OnTimer(UINT nIDEvent);
    	//}}AFX_MSG
    	afx_msg LRESULT OnGetDefID(WPARAM wp, LPARAM lp);
    	DECLARE_MESSAGE_MAP()
    In MyDlg.cpp
    Code:
    BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
    	//{{AFX_MSG_MAP(CMyDlg)
    	ON_WM_TIMER()
    	//}}AFX_MSG_MAP
    	ON_MESSAGE(DM_GETDEFID, OnGetDefID)
    END_MESSAGE_MAP()
    
    ...
    
    LRESULT CMyDlg::OnGetDefID(WPARAM wp, LPARAM lp) 
    {
      return MAKELONG(0,DC_HASDEFID); 
    }
    I thought this would handle the enter key, but that's what I get for trying to think.

    What's going on here?

    How can I get the enter key to stop shutting down the app?

  2. #2
    Join Date
    Mar 2004
    Location
    (Upper-) Austria
    Posts
    2,899

    Re: Problem with enter key

    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.
    I am not offering technical guidiance via email or IM
    Come on share your photo with us! CG members photo album!
    Use the Code Tags!

  3. #3
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Problem with enter key

    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.

  4. #4
    Join Date
    Sep 2003
    Location
    India
    Posts
    196

    Re: Problem with enter key

    Quote Originally Posted by lsvedin
    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...

    and even check this link may be u can get some help from this..
    http://www.codeguru.com/Cpp/W-D/disl...icle.php/c4965

    if i am not correct pls correct me gurus...

    venky

  5. #5
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Problem with enter key

    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).
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  6. #6
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: Problem with enter key

    Take a look at the following FAQ...

  7. #7
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: Problem with enter key

    To let enter work like 'Tab' key:
    Code:
    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:
    Code:
    #define NEXT_CONTROL() \
      CWnd* pWnd = GetFocus();\
      if(GetDlgItem(IDOK) != pWnd) \
      {
        NextDlgCtrl(); \
        return; \
      }
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  8. #8
    Join Date
    Oct 1999
    Location
    ks
    Posts
    523

    Re: Problem with enter key

    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
    Attached Files Attached Files
    Last edited by jim enright; March 26th, 2007 at 11:19 AM. Reason: improve

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured