Thread: Keystroke

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    82

    Keystroke

    Hey how do you make a your callback WindowProc() function detect when you press the 'Enter Key' from a single-line Edit Control?

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    70
    Its very insteresting......

    You are able to detect 'Enter Key' pressed event on Edit control from within your WinMain() function while you takes messages through GetMessage(...) or PeekMessage(...) function.

    You will get MSG structure through above two functions. MSG structure contains Window's handle and generated message. If message parameter of MSG structure is WM_KEYDOWN, wParam parameter of MSG structure is 13 and if hwnd parameter match with your edit control's handle then you can say that 'Enter Key' is pressed on Edit Control.

    To make callback WindowProc() function detect when you press the 'Enter Key' from a single-line Edit Control, I will give my opinion after thinking on it.
    Chintan R Naik

  3. #3
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    you need the ES_WANTRETURN and this only works with the ES_MULTILINE style.

    Why do you need a single line edit?

    Try changing the style to mutli line and not specifying the ES_AUTOVSCROLL style.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  4. #4
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    If you really want to do this, a useful technique is subclassing. This program demonstrates it, and how to catch a few useful keys.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    82
    Nevermind I figured out a simpler one.
    I had to opt for ES_MULTILINE though (but with no ES_AUTOVSCROLL).

    Code:
    LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
    {
      switch(Message) {
        case WM_COMMAND:
          if(HIWORD(wParam) == EN_CHANGE && GetAsyncKeyState(13) == (-32767)) {
            MessageBox(hwnd, "The Enter key has been pressed", "ALERT", MB_OK);
          }
        return 0;
    
        default:
          DefWindowProc(hwnd, Message, wParam, lParam);
          return 0;
      }
    }
    Thanks anyway guys
    Last edited by Rez; 07-03-2003 at 11:11 PM.

  6. #6
    Registered User
    Join Date
    Jun 2003
    Posts
    70

    if(HIWORD(wParam) == EN_CHANGE && GetAsyncKeyState(13) == (-32767)) {
    MessageBox(hwnd, "The Enter key has been pressed", "ALERT", MB_OK);


    I think in if() condition you will also have to check for edit control's handle. Because if you have more than 1 edit controls then how can you identify even for a particular edit control ??

    So.....I think you will also have to compare first parameter of WndProc() function. (i.e. HWND hwnd)
    Chintan R Naik

  7. #7
    Registered User Xei's Avatar
    Join Date
    May 2002
    Posts
    719
    Look up Subclassing. Its better.
    "What are you after - the vague post of the week award?" - Salem
    IPv6 Ready.
    Travel the world, meet interesting people...kill them.
    Trying to fix or change something, only guaruntees and perpetuates its existence.
    I don't know about angels, but it is fear that gives men wings.
    The problem with wanting something is the fear of losing it, or never having it. The thought makes you weak.

    E-Mail Xei

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help passing keystroke to program
    By snap-tech in forum C++ Programming
    Replies: 2
    Last Post: 02-01-2008, 04:41 AM
  2. Problem with keystroke stacking
    By Flakster in forum C++ Programming
    Replies: 13
    Last Post: 10-20-2005, 02:18 PM
  3. Keystroke Detection
    By Rainer in forum C++ Programming
    Replies: 8
    Last Post: 01-19-2004, 11:40 PM
  4. Intercept Keystroke
    By Onkel BeBu in forum C++ Programming
    Replies: 1
    Last Post: 10-08-2003, 10:10 AM
  5. keystroke processing
    By lambs4 in forum Windows Programming
    Replies: 3
    Last Post: 09-07-2003, 04:22 PM