Dialog always needs to see keypresses...

Posted on 1998-06-23
Last Modified: 2013-11-20
I have a dialog box containing a long scrollable RichEdit that contains the text of an agreement.  There are two buttons, Yes I Do, & No I Don't Accept the agreement.  The default button when the dialog comes up is NO.  Even though it is not standard windows interface design, I need the dialog to react to PageUp, PageDown, ArrowUp, ArrowDown, no matter which button (or box) is currently highlighted.
I've used Class Wizard to generate functions for  WM_ONCHAR & WM_ONKEYDOWN, but they never get called (yes, me newbie).  I know how to talk to the box to scroll the text but I can't trap the keys.  
Question by:Musashi
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4

Accepted Solution

Belgarat earned 60 total points
ID: 1318335
The keys are send directly to the focused control, not to the dialog window, so if you're trying to trap some keys there, your handlers will not get called.
The first approach is to subclass the RichText control (create a derived class from CRichText) and add appropriate handlers. Then you add a member variable bound to the richedit control and manually change its type to CYourRichText in the dialog's header.
Probably more efficient is to override your dialog's member function
BOOL PreTranslateMessage(MSG *pMsg)
You can watch for WM_KEYDOWN there and do whatever you wish. This will capture ALL keypresses regardless of what control is currently focused. If your function returns 1, the message will be discarded. If you return 0, the message will be dispatched by the standard procedures (see help on CWnd::PreTranslateMessage).


Author Comment

ID: 1318336
I found Article ID: Q117563 which directly addresses this question.  The title is "How to Trap WM_KEYDOWN Messages in a CDialog", and basically suggests to override the predefined overridable MFC hook function ProcessMessageFilter() to capture the messages before they get to the dialog box.

However, I won't be able to test this method vice the proposed answer until next week.  I'm new to the Expert's Exchange and would like some advice on how to handle the points&etc. if this method works for me. IE, Do I give you the points for giving me an answer or do I give myself the points somehow if I use my own answer.  Please understand I'm not trying to be stingy, just want to know the correct procedure.

Author Comment

ID: 1318337
Tested decreasing the points, will return to 65 no matter what.

Author Comment

ID: 1318338
That's odd, they rejected the decrease but not the comment.  I think I would have preferred an error message not a post.

Author Comment

ID: 1318339
That's odd, they rejected the decrease but not the comment.  I think I would have preferred an error message not a post.

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor ( Top Charts is a view in which you can set seve…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question