[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1247
  • Last Modified:

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?

1 Solution
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.

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

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 :)
dmaroffAuthor Commented:
Thanks everyone.  I used a combination of all of your answers.


Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now