Is it possible for KeyPress events in MFC?

  • Thread starter Thread starter sweetjones
  • Start date Start date
  • Tags Tags
    Events
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
6 replies · 16K views
sweetjones
Messages
44
Reaction score
0
I've been looking around for a good tutorial that deals with the KeyPress event handler for MFC VC++. I only see WM_OnKeyDown events. Are they the same? I want to be able to use the keyboard numbers in my editControl boxes. The MSDN isn't much help unfortunately. Thanx In Advance!
 
Physics news on Phys.org
bool WM_OnKey(const Key*);
bool WM_OnKeyDown(const Key*);
bool WM_OnKeyUp(const Key*);

are the usual suspects for what you want. Normally, WM_OnKeyUp is the one chosen to respond to the action. Some keyboard drivers will pulse certain keys when they are held, so that complicates things.
 
jim mcnamara said:
bool WM_OnKey(const Key*);
bool WM_OnKeyDown(const Key*);
bool WM_OnKeyUp(const Key*);

are the usual suspects for what you want. Normally, WM_OnKeyUp is the one chosen to respond to the action. Some keyboard drivers will pulse certain keys when they are held, so that complicates things.

Great! So to enable the number keys, do I make a range such as, "if (e.KeyChar >= '0' || e.KeyChar <= '9')" inside the WM_OnKeyUp()?
 
By number keys do you mean F1 - that returns two numbers.
Regular ascii keys return like 48 or '0' to the message pump when the zero key is pressed.
It is a pointer to a value: const Key* Key is a datatype in windows.h, as is bool
You get either TRUE or FALSE when you test with these functions.
Code:
value_to_test='0';
if (WM_OnKeyUp( &value_to_test) ==TRUE) {
       // do stuff
}

FWIW - when I did this stuff years ago MSDN had volumes of information... there are other event and notification testing calls as well. I think you might want to use notifications...

Anyway see:
http://msdn2.microsoft.com/en-us/library/ms645530(VS.85).aspx
 
jim mcnamara said:
By number keys do you mean F1 - that returns two numbers.
Regular ascii keys return like 48 or '0' to the message pump when the zero key is pressed.
It is a pointer to a value: const Key* Key is a datatype in windows.h, as is bool
You get either TRUE or FALSE when you test with these functions.
Code:
value_to_test='0';
if (WM_OnKeyUp( &value_to_test) ==TRUE) {
       // do stuff
}

FWIW - when I did this stuff years ago MSDN had volumes of information... there are other event and notification testing calls as well. I think you might want to use notifications...

Anyway see:
http://msdn2.microsoft.com/en-us/library/ms645530(VS.85).aspx

Thanks a lot for the help. I guess I need to do more reading on MFC to understand how exactly to use and write these functions. For me, sometimes MSDN is jibberish. LOL! Sometimes I feel overwhelmed with all of the information. Anyway, should I test your code in a WM_OnKeyUp function or in another function, or does it depend on my program? Thanks Again!
 
There's a lot to it. Your object has to have focus to receive events, for starters.
And you do NOT have to use MFC - which I found to cause as many issues as it solved.
Get a copy of Charles Petzold's Programming Windows - the one for Windows 98 and on.
http://books.google.com/books?id=7msBAAAACAAJ&dq=inauthor:Charles+inauthor:Petzold

No offense meant, but I think you need to start at the beginning. Instead of inside MFC.
 
Last edited by a moderator:
jim mcnamara said:
There's a lot to it. Your object has to have focus to receive events, for starters.
And you do NOT have to use MFC - which I found to cause as many issues as it solved.
Get a copy of Charles Petzold's Programming Windows - the one for Windows 98 and on.
http://books.google.com/books?id=7msBAAAACAAJ&dq=inauthor:Charles+inauthor:Petzold

No offense meant, but I think you need to start at the beginning. Instead of inside MFC.

True to a certain extent. I just learn and understand better with examples. Unfortunately I'm learning this on my own so it's taking me a while. I'm working on a Fraction Calculator which is basically complete. I still have a few things to workout such as, giving the user the option to input everything via the keyboard instead of just clicking all the buttons for input. Basically making it more user friendly.

@jim mcnamara- check your PM
 
Last edited by a moderator: