Thread: WM_CHAR problems

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    552

    WM_CHAR problems

    how do I determine which key was pressed when the WM_CHAR message is sent.

    I tried this, as was suggested to me, but It doesnt work
    Code:
    if ((int)wParam == 13) {
      int i = 0;  // breakpoint added here, not reached
    }
    I also tried many other permutations of 'wParam == ', but nothing works. I checked MSDN, but they dont give any examples and their explanations seems incomplete. Any help would be appreciated.
    C Code. C Code Run. Run Code Run... Please!

    "Love is like a blackhole, you fall into it... then you get ripped apart"

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    In your WM_CHAR message handler switch on the wParam then have case statements for the desired letters.....example:

    Code:
    .
    .
    .
    case WM_CHAR:
      
      switch( wParam )
      {
        case 'W':
        case 'w':
          MessageBox(hWnd,"w was pressed.","Info",MB_OK);
          break;
    
        case 'e':
          // e was hit
          break;
      }
      break;
    .
    .
    .

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    I guess I should have been more specific. I need to detect enter being pressed, but that doesnt seem to work. Ive also tried VK_RETURN. Any ideas?
    C Code. C Code Run. Run Code Run... Please!

    "Love is like a blackhole, you fall into it... then you get ripped apart"

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    Setting the edit to multiline seems to solve the problem
    C Code. C Code Run. Run Code Run... Please!

    "Love is like a blackhole, you fall into it... then you get ripped apart"

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    329
    Have you tried displaying the value you receive when enter is pressed?
    Code:
    char *buf=new char[10];
    sprintf(buf, "%i\n", wParam);
    MessageBox(0, buf,"",0);

  6. #6
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Return, and TAB incidently, have special meaning for edit boxes, and other controls. To get return and tab messages for a single line edit box you can subclass it.

    The attached example is a quicky I put together, but it demostrates the technique.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  7. #7
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    >>Setting the edit to multiline seems to solve the problem

    ES_MULTILINE | ES_WANTRETURN


    Also there is a list of #defines for the (virtual) keys in winuser.h

    ie VK_TAB, VK_RETURN, VK_ESCAPE
    "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

  8. #8
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    I originally wanted to detect return in a combo box, but nothing I tried worked. Any ideas on how I could do that? I tried subclassing it but the return char was never sent.
    C Code. C Code Run. Run Code Run... Please!

    "Love is like a blackhole, you fall into it... then you get ripped apart"

  9. #9
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    i didn't really read through it, but this is what you need. its from the win32 programmers reference.

    Code:
    #define WM_TAB (WM_USER)  
    #define WM_ESC (WM_USER + 1) 
    #define WM_ENTER (WM_USER + 2) 
     
    HWND hwndMain; 
    HWND hwndEdit; 
    WNDPROC lpfnEditWndProc; /* original window procedure for */ 
                             /*   the combo box edit windows  */ 
     
    int cyToolbar;           /* toolbar window height        */ 
     
    /******************************************************** 
     
        FUNCTION:   ToolbarWindowProc 
     
        PURPOSE:    Window procedure for the toolbar window 
    
     
    *********************************************************/ 
     
    LRESULT CALLBACK ToolbarWindowProc(hwnd, msg, wParam, lParam) 
    HWND hwnd; 
    UINT msg; 
    WPARAM wParam; 
    LPARAM lParam; 
    { 
        static HWND   hwndEdit1; 
        static HWND   hwndEdit2; 
        static HWND   hwndCombo1; 
        static HWND   hwndCombo2; 
        . 
        . 
        . 
        POINT       pt; 
        DWORD       dwBaseUnits; 
        HWND        hwndCombo; 
        DWORD       dwIndex; 
     
        switch (msg) { 
    
            case WM_CREATE: 
     
                /* Create two combo box child windows. */ 
     
                dwBaseUnits = GetDialogBaseUnits(); 
     
                hwndCombo1 = CreateWindow("COMBOBOX", "", 
                    CBS_DROPDOWN | WS_CHILD | WS_VISIBLE, 
                    (6 * LOWORD(dwBaseUnits)) / 4, 
                    (2 * HIWORD(dwBaseUnits)) / 8, 
                    (100 * LOWORD(dwBaseUnits)) / 4, 
                    (50 * HIWORD(dwBaseUnits)) / 8, 
                    hwnd, NULL, hinst, NULL); 
    
     
                hwndCombo2 = CreateWindow("COMBOBOX", "", 
                    CBS_DROPDOWN | WS_CHILD | WS_VISIBLE, 
                    (112 * LOWORD(dwBaseUnits)) / 4, 
                    (2 * HIWORD(dwBaseUnits)) / 8, 
                    (100 * LOWORD(dwBaseUnits)) / 4, 
                    (50 * HIWORD(dwBaseUnits)) / 8, 
                    hwnd, NULL, hinst, NULL); 
     
                /* Get the edit window handle for each combo box. */ 
     
                pt.x = 1; 
                pt.y = 1; 
    
                hwndEdit1 = ChildWindowFromPoint(hwndCombo1, pt); 
                hwndEdit2 = ChildWindowFromPoint(hwndCombo2, pt); 
     
                /* 
                 * Change the window procedure for both edit windows 
                 * to the subclass procedure. 
                 */ 
     
     
                lpfnEditWndProc = (WNDPROC) SetWindowLong(hwndEdit1, 
                    GWL_WNDPROC, (DWORD) SubClassProc); 
     
                SetWindowLong(hwndEdit2, GWL_WNDPROC, 
                    (DWORD) SubClassProc); 
    
     
                break; 
     
            case WM_SETFOCUS: 
                SetFocus(hwndCombo1); 
                break; 
     
            case WM_TAB: 
                if (GetFocus() == hwndEdit1) 
                    SetFocus(hwndCombo2); 
                else 
                    SetFocus(hwndCombo1); 
                break; 
     
            case WM_ESC: 
     
                hwndCombo = GetFocus() == hwndEdit1 ? 
                    hwndCombo1 : hwndCombo2; 
     
                /* Clear the current selection. */ 
    
     
                SendMessage(hwndCombo, CB_SETCURSEL, 
                    (WPARAM) (-1), 0); 
     
                /* Set the focus to the main window. */ 
     
                SetFocus(hwndMain); 
                break; 
     
            case WM_ENTER: 
                hwndCombo = GetFocus() == hwndEdit1 ? 
                    hwndCombo1 : hwndCombo2; 
                SetFocus(hwndMain); 
     
                /* If there is no current selection, set one. */ 
     
                if (SendMessage(hwndCombo, CB_GETCURSEL, 0, 0) 
    
                        == CB_ERR) { 
                    if (SendMessage(hwndCombo, WM_GETTEXT, 
                            sizeof(achTemp), (LPARAM) achTemp) == 0) 
                        break;      /* empty selection field */ 
                    dwIndex = SendMessage(hwndCombo, 
                        CB_FINDSTRINGEXACT, (WPARAM) (-1), 
                        (LPARAM) achTemp); 
     
                    /* Add the string, if necessary, and select it. */ 
     
                    if (dwIndex == CB_ERR) 
    
                        dwIndex = SendMessage(hwndCombo, CB_ADDSTRING, 
                            0, (LPARAM) achTemp); 
                    if (dwIndex != CB_ERR) 
                        SendMessage(hwndCombo, CB_SETCURSEL, 
                            dwIndex, 0); 
                } 
                break; 
     
                . 
                .   /* Process additional messages. */ 
                . 
     
            default: 
                return DefWindowProc(hwnd, msg, wParam, lParam); 
        } 
        return 0; 
    
    } 
     
     
    /******************************************************** 
     
        FUNCTION:   SubClassProc 
     
        PURPOSE:    Process TAB and ESCAPE keys, and pass all 
                    other messages to the class window 
                    procedure. 
     
    *********************************************************/ 
     
    LRESULT CALLBACK SubClassProc(hwnd, msg, wParam, lParam) 
    HWND hwnd; 
    UINT msg; 
    WPARAM wParam; 
    LPARAM lParam; 
    { 
        switch (msg) { 
            case WM_KEYDOWN: 
    
                switch (wParam) { 
                    case VK_TAB: 
                        SendMessage(hwndToolbar, WM_TAB, 0, 0); 
                        return 0; 
                    case VK_ESCAPE: 
                        SendMessage(hwndToolbar, WM_ESC, 0, 0); 
                        return 0; 
                    case VK_RETURN: 
                        SendMessage(hwndToolbar, WM_ENTER, 0, 0); 
                        return 0; 
                } 
                break; 
     
            case WM_KEYUP: 
    
            case WM_CHAR: 
                switch (wParam) { 
                    case VK_TAB: 
                    case VK_ESCAPE: 
                    case VK_RETURN: 
                        return 0; 
                } 
        } 
     
        /* 
         * Call the original window procedure for default 
         * processing. 
         */ 
     
        return CallWindowProc(lpfnEditWndProc, hwnd, 
            msg, wParam, lParam); 
    }
    basically, the idea is, get the handle of the edit control and subclass.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 06:35 AM
  3. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  4. Rendering problems (DirectX?)
    By OnionKnight in forum Tech Board
    Replies: 0
    Last Post: 08-17-2006, 12:17 PM
  5. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 09:19 PM