Link to home
Start Free TrialLog in
Avatar of dmaroff
dmaroff

asked on

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?

Thanks,
-Dan
Avatar of bbousquet
bbousquet
Flag of Canada image

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.
Avatar of dmaroff
dmaroff

ASKER

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.

-Dan
HI,

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.
VinExpert

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 );

DECLARE_MESSAGE_MAP()

In .cpp file you need:

BEGIN_MESSAGE_MAP(CICSOutputEditCtrl, CRichEditCtrl)
      //{{AFX_MSG_MAP(CChessMachineView)
      ON_WM_CHAR()
      ON_WM_KEYDOWN()
      //}}AFX_MSG_MAP
END_MESSAGE_MAP()

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.
ASKER CERTIFIED SOLUTION
Avatar of ScottyDawg
ScottyDawg

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 dmaroff

ASKER

Thanks everyone.  I used a combination of all of your answers.

-Dan