Keyboard messages in a Modal Dlg

 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,
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
aguevara020397Author Commented:
I still don't know how to do it though.  Can you tell me where I can find and example ??
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.
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

aguevara020397Author Commented:
  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,
I will see what I can do for you. Give me about 20 hours. Good night.
aguevara020397Author Commented:

   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.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.