0

Keyboard Enter key not working in MFC Dialog Box ? I have one MFC Login dialog box when I enter Username and password and hit Enter key the dialog box close down while when I use mouse to click on login it login's successfully.

What I need to do for making keyboard key work in MFC ?

Swapnil Gupta
  • 8,751
  • 15
  • 57
  • 75
  • 1
    The Enter key is translated to IDOK. When you press that key, it's similar to click on the Ok button. Normally OnOK() is called. Probably that helps to remove Enter as "active" key. – harper Nov 18 '10 at 12:26
  • @harper : I have removed OK button from my MFC dialog box as i dont need it. – Swapnil Gupta Nov 18 '10 at 12:31

3 Answers3

3

Implement the OnOK() and/or the OnCancel() methods and don't call the base methods there. (those are virtual methods which you can simply overload).

Just because you removed the buttons doesn't mean you don't have to handle the events! The OnOK() is triggered by the Enter key, and OnCancel() by the Escape key for dialogs.

Himanshu
  • 31,810
  • 31
  • 111
  • 133
Stefan
  • 43,293
  • 10
  • 75
  • 117
  • The problem is i have removed OK and cancel button as i dont need them i have placed one login and close button in my dialog box but i am placing a image on top of login button for that i need to set Ownerdraw property = true for button to merge login image with button,when i m doing like that default button property becomes false of login button. – Swapnil Gupta Nov 19 '10 at 03:44
  • So why does the default button property become false? That sounds like the problem to me. – jussij Nov 23 '10 at 07:13
0

in this case handle the enter key in the pretranslate() method of your db.

megha
  • 170
  • 2
  • 10
  • Now i am using pretranslate() method like this :BOOL CNDSConnectDlg::PreTranslateMessage(MSG* pMsg) { base class if(pMsg->message==WM_KEYDOWN) { if(pMsg->wParam==VK_RETURN || pMsg->wParam==VK_ESCAPE) pMsg->wParam=NULL ; } return CDialog::PreTranslateMessage(pMsg); } now when i press enter window does not get closed but now i want to call login method on press of enter , when i put my login method in pretransalte() everytime it gets called. – Swapnil Gupta Nov 19 '10 at 08:25
0

Go to the resource view and make sure you have no entry for VK_RETURN with no modifier in Accelerators. This might be a cause too.