Keyboard messages in a Modal Dlg

Posted on 1997-06-12
Last Modified: 2013-11-20
 I have a modal dialog box which in which I would like to do "something" when a key is pressed (i.e. F4, pageup, page down, +, -, 1, 2, 3).  I tried onkeydown and onchar, but those do not work.

  In addition, I would like to know if there is a way to disable or enable certain keys.  There are cases where I only want the user to be able to use the numeric keypad and not allow for any alpha characters.

  If you have any information, email me at

Have a good day,
Question by:aguevara020397
  • 3
  • 3

Accepted Solution

mbhakta earned 20 total points
ID: 1302070
The onkeydown and onchar didnot work because the message was not routing to your CDialog but going to another control on the dialog say a button or whatever belongs to tab index 1.  You will have to carefully work out the messaging for dialogs as it works a bit different from other windows. There is no problem in achieving what you are planning to do, but take care you don't get lost in the CDialog messaging maze.

Author Comment

ID: 1302071
I still don't know how to do it though.  Can you tell me where I can find and example ??

Expert Comment

ID: 1302072
Actually, I did no coding at all. What I just did is override the OnKeyDown and OnChar for the dialog. Then added some trace prints on these calls. Ran it , pressed some keys did not work. I realized the focus was on a button Ok and not on the dialog. Delete all controls on top of the dialog and boom I could see the messages passing thru and so the trace print. It is pretty simple try it. Also I just remembered one of MSJ's back issues had this article may be 1 year ago. Check their archives.
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.


Author Comment

ID: 1302073
  I download several issues of the MSJ source code from 1996 and could not find it.  I also saw that you had answered the same question before and you had stated to "Check the MSJ issues a couple of months ago (say from March 96). There was an article explaining how to do keystrokes for CDialogs ...."  I downloaded the march,may,june and july issues and could not find it.
  Would it possible for you to look at your back issues and find it for me ? I am not subscribed to MSJ so I can't very well do so.

  Have a good day,

Expert Comment

ID: 1302074
I will see what I can do for you. Give me about 20 hours. Good night.

Author Comment

ID: 1302075

   I found an example on how to trap keyboard messages while in a CDialog, the example essentially suggested to do the following

(1) Create a CDialog pointer in your main App class (CWinApp)
(2) Initialize it to NULL in the InitInstance
(3) Set to a valid value in the constructor of your CDialog class
(4) Set back to NULL on the onDestroy procedure of your CDIalog class
(5) Define virtual BOOL ProcessMessageFilter( int code, LPMSG lpMsg ); on the CWinApp header file
(6) Insert the ProcessMessageFilter( int code, LPMSG lpMsg ) in the CWin App CPP file
(7) In the ProcessMessageFilter function, check to see if the CDialog pointer is NON-NULL, if it is then we know a key has been pressed in the CDialog class,
you can do a if (lpMsg->message==WM_KEYDOWN) to determine if a key has been pressed.

I still can't figure out how to find out what key specifically has been pressed, I am looking into that since based on the key pressed (PageUp, PageDown, +, -,
keys) I would like to perform different functions.

If you have any ideas on how to decipher the key pressed, please let me know.


Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Question regarding Copy/Paste 16 87
string initialization in java 11 103
NotAlone Challenge 20 72
fix34  challenge 9 98
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: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now