Capturing 'enter' key in combo box

This is a discussion on Capturing 'enter' key in combo box within the Windows Programming forums, part of the Platform Specific Boards category; This is a question I probably should know the answer to, having been programming in MFC for nearly a year ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Location
    England
    Posts
    121

    Capturing 'enter' key in combo box

    This is a question I probably should know the answer to, having been programming in MFC for nearly a year now, but so far I've managed to avoid it. Is there any way to capture the enter key in the edit box part of a dropdown box without setting up a message loop (so far I process messages using MFC's automatic OnBtnPress() etc.) and without subclassing the combo box. Also how do I send a message to a window to emulate a key being pressed (specifically the 'down' cursor key), and does anyone know a good reference for WM_ messages for individual keys (I've looked on google, and MSDN).

    If anyone could enlighten me on any of these questions it would really help a few things I need to do, thanks.

    -edit-
    100th post!

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,643
    1) "no". You either have to subclass it or fiddle with message loops.

    2) SendMessage(HWND, UINT, WPARAM, LPARAM), ie: SendMessage(hEdit, WM_LBUTTONDOWN, 0, 0);

    3) The VK_ codes are all in your winuser.h header.

    Also, you should download the Win32 searchable reference ".hlp" file, as you will find it invaluable in your quest for knowledge. Download it here.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  3. #3
    Registered User
    Join Date
    Sep 2001
    Location
    England
    Posts
    121
    Thanks for the help, I've managed to capture the VK_RETURN message in PreTranslateMessage() by subclassing a combo box. My problem is in writing the code to let this return be picked up by an OnSelChange function. I think this would be achieved by using SendMessage to send WM_SELCHANGE from the combo box to its parent, is this correct, or do I send the message to the main window of my app?

  4. #4
    Registered User
    Join Date
    Sep 2001
    Location
    England
    Posts
    121
    Thought I was getting somewhere, but I still cant get it to work. Learnt a lot about windows messaging and related functions for this, and I cant understand why the following code isnt triggering CCombosParent::OnSelChangeCombo();:

    Code:
    BOOL CEntCombo::PreTranslateMessage(MSG* pMsg)
    {
    	if(pMsg->message == WM_KEYDOWN && pMsg->wParam ==VK_RETURN) {
    		switch(pMsg->wParam)
    		{
    		case VK_RETURN:
    			GetParent()->SendMessage(WM_COMMAND, 
    						CBN_SELCHANGE, 
    						(LPARAM)this->m_hWnd);
    			break;
    		default:
    			CComboBox::PreTranslateMessage(pMsg);
    		}
    
    	}
    	else{
    	return CComboBox::PreTranslateMessage(pMsg);
    	}
    }

    Any help greatly appreciated

  5. #5
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    To synthesise a WM_COMMAND message you need to set the correct paramaters; ie from msdn:
    Code:
    LRESULT CALLBACK WindowProc(
                                HWND hwnd,       // handle to window
                                WM_COMMAND,      // the message to send
                                WPARAM wParam,   // notification code and id
                                LPARAM lParam);  // handle to control (HWND)
    So to send your message, try:
    Code:
    GetParent()->SendMessage(WM_COMMAND, 
                             MAKEWPARAM(nID,CBN_SELCHANGE), 
                             (LPARAM)m_hWnd);
    where nID is the resource identifier of your control.

    Hope that helps.

    edit: formatting
    Last edited by Ken Fitlike; 11-16-2002 at 08:32 PM.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,643
    Well, I believe the VK_ code is going to be in the LOWORD of the WPARAM:

    Code:
    BOOL CEntCombo::PreTranslateMessage(MSG* pMsg)
    {
     if(pMsg->message != WM_KEYDOWN 
        && LOWORD(pMsg->wParam) != VK_RETURN)
      return CComboBox::PreTranslateMessage(pMsg);
    
     GetParent()->
      SendMessage(
       WM_COMMAND, 
       MAKEWPARAM(/*??*/,CBN_SELCHANGE), 
       (LPARAM)m_hWnd);
    
     return TRUE;
    }
    Last edited by Sebastiani; 11-16-2002 at 11:18 PM.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  7. #7
    Registered User
    Join Date
    Sep 2001
    Location
    England
    Posts
    121
    Thanks again, it's all working now. I took
    WPARAM wParam, // combo box identifier, CBN_SELCHANGE
    on MSDN to mean that the wParam should be the message identifier specific to the combo box, eg CBN_SELCHANGE. Just misunderstood it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help With a BlackJack Program in C
    By Jp2009 in forum C Programming
    Replies: 15
    Last Post: 03-30-2009, 10:06 AM
  2. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  3. Combo Box Extended
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 11-15-2001, 08:04 AM
  4. BST/Red and Black Tree
    By ghettoman in forum C++ Programming
    Replies: 0
    Last Post: 10-24-2001, 10:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21