Link to home
Start Free TrialLog in
Avatar of azar
azar

asked on

Allowing a <RETURN> to do a NextDlgCtrl( )

I need to have my Windows 3.1 app (built using MSVC++ 1.52) allow the user to hit the <ENTER> key to jump to the next control in the TAB order on the dialog box.  I want to let the person use the <ENTER> key also.

I would like the code to do this.  I have tried -


void NewType::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
      if (nChar == VK_RETURN)
      {
            NextDlgCtrl( );
      }            
      CDialog::OnChar(nChar, nRepCnt, nFlags);
}

int NewType::OnCharToItem(UINT nChar, CListBox* pListBox, UINT nIndex)
{
      if (nChar == VK_RETURN)
      {
            NextDlgCtrl( );
      }            
      return CDialog::OnCharToItem(nChar, pListBox, nIndex);
}

void NewType::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
      CDialog::OnKeyDown(nChar, nRepCnt, nFlags);
}
                                                                   
But it never calls these member functions.  What am I missing?

Thanks
 
Avatar of azar
azar

ASKER

Edited text of question
Your dialog will never get these messages because the keyboard focus is on a specific child control such as an edit control.

One idea would be to create a default push button on your dialog that is hidden. Then when the user presses the enter key, the push button is pressed. Use a handler in your dialog to catch this and use a

  SendMessage(GetSafeHwnd(), WM_NEXTDLGCTL, 0, FALSE);

to simulate a tab being pressed.


Avatar of azar

ASKER

The focus isn't the issue.    I am setting the focus to the first object and want the <ENTER> key to move you to the next one.  I'd be happy to have the main dialog box window grab the keyboard input  and then check for which  control has the focus if necessary

If anyone has an answer, please post the few lines necessary to find out that the user hit the Enter key and then do the NextDlgItem command to move to the next control.

thanks
The focus IS the issue.  The dialog item has the focus.  So the dialog item, which is a window, is getting the WM_CHAR and WM_KEYDOWN messages.  You are looking for those messages in the dialog window's procedure.  The dialog window will not get the messages because it doesn't have te focus.

One way around this would be to sub-class the controls so that you can look for the enter keystroke in each of the controls' window procedures.
By "dialog item" I mean a control on the dialog window.  Sorry about the poot terminology.
Avatar of azar

ASKER

What about just passing the key entry message up to the parent, the dialog box, and have it handle the entry.  

I definately don't want to create 50 'false buttons' to allow every dialog control just to let the user move around using the ENTER key rather than the TAB.
The only way to pass the  key message up to the parent is by sub-classing the control.  If you are going to sub-class the control, you might as well just handle it in the control's window procedure.  

The false buttons Thui proposed would be simpler, but kinda cludgey.  It might cause som suprises later.  
ASKER CERTIFIED SOLUTION
Avatar of Srw
Srw

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of azar

ASKER

That nailed it!  I had already overridden the OnOK and I had done pretty much what you said, I had the code a bit/lot more convoluted, so the focus wan't getting snagged for a few cases.

This is simple, elegant, and exactly what I was asking for.   I 'KNEW' it could be done easily  (I had been shown this by someone a long time ago) but forgot how, and with people saying there wasn't a simple way, I kept trying to remember since I knew there was.

THANK YOU VERY MUCH.   The key here was that the users wanted to use the <ENTER> key and not the <TAB> so I wanted to get that working for them.
That is EXACTLY the same reason I needed.  I was writing a windows program to replace an old dos one.  The users were very computer illiterate, and it *really* needed to use the same keystrokes.

I also use this method when I don't want the return/escape keys to do anything at all.
Avatar of azar

ASKER

Same here.  The folks are completely computer illiterate and the idea of hitting <TAB> was something that was terribly confusing to them.  This will makes things much easier for them.  Thanks again.