Is it possible for KeyPress events in MFC?

  • Thread starter Thread starter sweetjones
  • Start date Start date
  • Tags Tags
    Events
Click For Summary
The discussion centers around the use of the KeyPress event handler in MFC VC++, specifically the differences between WM_OnKeyDown and WM_OnKeyUp events. Participants clarify that WM_OnKeyUp is typically used to respond to key actions, but complications can arise due to keyboard drivers that pulse keys when held. To enable number keys in edit controls, a conditional check using the character values is suggested. There is an emphasis on the importance of understanding the MFC framework and the need for the object to have focus to receive events. Additionally, resources like MSDN and Charles Petzold's "Programming Windows" are recommended for further learning. The original poster expresses a desire for more practical examples to aid their understanding while developing a Fraction Calculator that allows keyboard input.
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!
 
Technology 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:

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
Replies
58
Views
6K
Replies
4
Views
767
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K