Is it possible for KeyPress events in MFC?

  • Thread starter sweetjones
  • Start date
  • Tags
    Events
In summary, the conversation discusses using the KeyPress event handler for MFC VC++ and mentions WM_OnKeyDown, WM_OnKeyUp, and WM_OnKey as potential functions to use. The speakers also discuss the use of notifications and suggest referencing Charles Petzold's book for further understanding. They also mention the need for the object to have focus to receive events and suggest starting at the beginning to better understand the concept.
  • #1
sweetjones
44
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
  • #2
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.
 
  • #3
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()?
 
  • #4
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
 
  • #5
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!
 
  • #6
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:
  • #7
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:

Related to Is it possible for KeyPress events in MFC?

1. Can KeyPress events be used in MFC?

Yes, KeyPress events can be used in MFC (Microsoft Foundation Class) applications. MFC supports various types of KeyPress events, such as OnKeyDown, OnKeyUp, and OnChar. These events can be used to detect and respond to user keyboard input.

2. How do I handle KeyPress events in MFC?

To handle KeyPress events in MFC, you need to override the OnKeyDown, OnKeyUp, or OnChar function in your MFC application's class. This function will be called when a key is pressed, released, or when a character is typed, respectively. You can then add your desired code to handle the event.

3. Can I customize the behavior of KeyPress events in MFC?

Yes, you can customize the behavior of KeyPress events in MFC by using the WM_KEYDOWN, WM_KEYUP, and WM_CHAR messages. These messages are sent to the window's message handler when a key is pressed, released, or when a character is typed, respectively. You can handle these messages and perform custom actions based on the key pressed.

4. Are KeyPress events supported in all MFC controls?

Yes, KeyPress events are supported in most MFC controls, including buttons, edit boxes, list boxes, and combo boxes. However, some controls may have specific behaviors for certain keys, such as the arrow keys or tab key. It is important to check the documentation for the specific control you are using to determine its KeyPress event behavior.

5. Can I use KeyPress events in MFC for international keyboard input?

Yes, MFC supports KeyPress events for international keyboard input. The OnChar function can be used to handle characters from different languages and character sets. Additionally, MFC provides the CKeyMap class, which allows you to map virtual key codes to specific characters, making it easier to handle international keyboard input.

Similar threads

Replies
6
Views
826
Replies
58
Views
4K
  • Set Theory, Logic, Probability, Statistics
Replies
12
Views
2K
  • Programming and Computer Science
Replies
2
Views
3K
Replies
5
Views
987
  • Science Fiction and Fantasy Media
Replies
13
Views
1K
Replies
2
Views
131
  • Programming and Computer Science
Replies
6
Views
1K
Replies
90
Views
5K
Replies
1
Views
871
Back
Top