Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Is it possible for KeyPress events in MFC?

  1. Jan 24, 2008 #1
    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!!!
     
  2. jcsd
  3. Jan 24, 2008 #2

    jim mcnamara

    User Avatar
    Science Advisor
    Gold Member

    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.
     
  4. Jan 24, 2008 #3
    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()?
     
  5. Jan 24, 2008 #4

    jim mcnamara

    User Avatar
    Science Advisor
    Gold Member

    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 (Text):

    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
     
  6. Jan 24, 2008 #5
    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!!!
     
  7. Jan 24, 2008 #6

    jim mcnamara

    User Avatar
    Science Advisor
    Gold Member

    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.
     
  8. Jan 24, 2008 #7
    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
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Is it possible for KeyPress events in MFC?
Loading...