Using OnChar

I setup a dialog box and I want to receive WM_CHAR messages. so I used the MFC built-in OnChar() member function to do this.  Yet Im not receiving any messages when I do hit a key.  What I really want is to receive a message when the user presses the "DELETE" key.  Yet I put a breakpoint in the function, hit keys when the dialog was up and the function never got called.  I thought maybe the dialog box has an option to setup "key events" in the resource file, but I didn't find an option for that.

Anyone have any ideas?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Check out the DS_CONTROL style (it's in the 'styles' panel in the dialog's resource panel). I'm not 100% sure though.

You can use the CWnd::PretranslateMessage(...) overriden in your dialog's class. You shoud wait for the WM_KEYDOWN message and analize it if the DELETE key is pressed. Don't forget to call the default implemetation of the function.
dmaroffAuthor Commented:
Thanks kboros, but I realize I can use PretranslateMessage(...) but I really want to know why the OnChar() doesn't work, when it should work.  I dont want to examine and filter out all the window's messages and figure out the key codes through the use of a message structure when OnChar() does this for you very easily.

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Vinayak KumbarSr Program ManagerCommented:

No, U can't do it using OnChar function anyway. Bcoz, delete key press never triggers WM_CHAR message. It causes WM_KEYDOWN message which U have to trap using PreTranslateMessage() function as
.....::PreTranslateMassage(MSG* pMsg)
if((pMsg->message == WM_KEYDOWN) && (pMsg->wParam == 0x53)
//process the delete key
return CMDIFrameWnd::PreTranslateMessage(pMsg);

But the basic question is why U need that for a dialog?. it may be done for any control on the dialog say edit, listcontrol, listbox....I strongly think that u need it for any one of those controls and u want to do it in a dialog class.

If that is the case, then say u want to do it for the edit control. Then add the  member variable to that of CMyEdit. here CMyEdit is the derived class of CEdit using classwizard. Then map the WM_CHAR or WM_KEYDOWN message to it. Then it will work.

Try it out.

Bill NolanOwner, Lead Technology ProgrammerCommented:
As VinExpert says, WM_CHAR isn't sent by certain keys, I believe Delete is one of them.  Check the help on WM_CHAR and WM_KEYDOWN.
Anyway, here's a way to do it:

In .h file you need:

afx_msg void OnChar( UINT nChar, UINT nRepCnt, UINT nFlags );
afx_msg void OnKeyDown( UINT nChar, UINT nRepCnt, UINT nFlags );


In .cpp file you need:

BEGIN_MESSAGE_MAP(CICSOutputEditCtrl, CRichEditCtrl)

And then, obviously, you need to include the code for these functions.  Just check 'nChar' against an ascii code list to find the right codes.

Good luck.
Handle the WM_GETDLGCODE message.


UINT CTest2Dlg::OnGetDlgCode()
      UINT nCode = CDialog::OnGetDlgCode();

      return nCode | DLGC_WANTCHARS;

Now all OnChar message will be routed to you :)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dmaroffAuthor Commented:
Thanks everyone.  I used a combination of all of your answers.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.