Is it possible for KeyPress events in MFC?

  • Thread starter Thread starter sweetjones
  • Start date Start date
  • Tags Tags
    Events
Click For Summary

Discussion Overview

The discussion revolves around handling KeyPress events in Microsoft Foundation Class (MFC) applications, specifically focusing on the use of keyboard input in edit control boxes. Participants explore the differences between various key event handlers and share their experiences and challenges with MFC documentation.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant seeks clarification on the KeyPress event handler in MFC, noting a lack of resources on this topic and confusion between WM_OnKeyDown and other key events.
  • Another participant suggests using WM_OnKeyUp to respond to key actions, mentioning complications with certain keyboard drivers that may affect key input.
  • There is a proposal to check if the key character falls within a specific range to enable number key input in the WM_OnKeyUp function.
  • Discussion includes a clarification about the return values of key event functions and the nature of the Key datatype in Windows.
  • One participant expresses frustration with the MSDN documentation, describing it as overwhelming and difficult to understand, while seeking guidance on where to implement code for key handling.
  • Another participant emphasizes the importance of focus for receiving key events and suggests that MFC may not be necessary, recommending a foundational text on Windows programming instead.
  • A participant shares their personal learning journey, indicating they are developing a Fraction Calculator and wish to improve keyboard input functionality.

Areas of Agreement / Disagreement

Participants express differing views on the necessity of using MFC for handling key events, with some advocating for alternative approaches. There is no consensus on the best method for implementing KeyPress event handling, and the discussion remains unresolved regarding the optimal strategies.

Contextual Notes

Some participants note that the effectiveness of key event handling may depend on the specific program context and the need for objects to have focus to receive events. There are also references to potential complications with keyboard drivers and the varying interpretations of documentation.

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
4K
Replies
58
Views
7K
Replies
4
Views
1K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 12 ·
Replies
12
Views
5K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K