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.
Independent Software Vendors: 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!


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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
set up trigger (or something) to rename files (starting with ATL) in a linux directory 6 56
dog bark java program 15 121
Doc'in system (example?) BA 7 120
Not needed 13 129
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…
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit If you want to manage em…

733 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