Catching the "Enter" key in a CListCtrl (and other controls)

In order to catch the "Enter" key, and other special keys in a CListCtrl (or any control for that matter) it is necessary to derive your own class and add a handler for the WM_GETDLGCODE message. To allow all special keys to be processed by the control add the following:
UINT SMUPPGrid::OnGetDlgCode()
{ 
	UINT result = CListCtrl::OnGetDlgCode();
	result = result | DLGC_WANTALLKEYS;
	return result;
}

Other options include (from MSDEV documentation):

  • DLGC_WANTALLKEYS All keyboard input.
  • DLGC_WANTARROWS Arrow keys.
  • DLGC_WANTCHARS WM_CHAR messages.
  • DLGC_WANTMESSAGE All keyboard input. The application passes this message on to the control.
  • DLGC_WANTTAB TAB key.
  • DLGC_BUTTON Button (generic).
  • DLGC_DEFPUSHBUTTON Default pushbutton.
  • DLGC_HASSETSEL EM_SETSEL messages.
  • DLGC_UNDEFPUSHBUTTON No default pushbutton processing. (An application can use this flag with DLGC_BUTTON to indicate that it processes button input but relies on the system for default pushbutton processing.)
  • DLGC_RADIOBUTTON Radio button.
  • DLGC_STATIC Static control.

This is a relatively simple process but it is not really all that intuitive or well documented.

Last updated: 9 May 1998


Comments

  • DLGC_UNDEFPUSHBUTTON: is this an undocumented feature?

    Posted by Legacy on 03/29/1999 12:00am

    Originally posted by: newsoft

    When handling WM_GETDLGCODE, this is the behavior
    
    that I observed (i'm using NT 4.0 workstation SP4
    and Visual C++ 5.0 SP3):

    return DLGC_UNDEFPUSHBUTTON;
    // when this button gets the focus, the border
    // drawn with DrawFrameControl() is drawn with the
    // default black rectangle around it (as a default
    // button) and the regular default control (the OK
    // button usually) is drawn like a non-normal control.
    // Note that this affects only the DRAWING of buttons,
    // the enter key still goes to the default control
    // (the OK button - dismissing the dialog) even
    // though it's not drawn with the black rectangle
    // around it.

    return DLGC_DEFPUSHBUTTON;
    // when this button gets the focus, the enter
    // key will generate a WM_BUTTONDOWN event.
    // The key will not go the the usually default
    // button (the OK button usually).

    return DLGC_UNDEFPUSHBUTTON | DLGC_DEFPUSHBUTTON;
    // this seems to make an owner-drawn button behave
    // (look & feel) like a non owner-drawn button
    // (If using DrawFrameControl() to draw the frame).

    DLGC_BUTTON doesn't seem to have any effect if OR'ed
    with the above.

    Reply
  • Catching Enter in combobox

    Posted by Legacy on 10/26/1998 12:00am

    Originally posted by: Vadim

    This doesn't work with combobox. To catch Enter in combobox 
    
    override PreTranslateMassage():

    if (pMsg->message == WM_KEYDOWN) {
    if ((int)pMsg->wParam == VK_RETURN) {
    do your processing
    }
    }

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Today's agile organizations pose operations teams with a tremendous challenge: to deploy new releases to production immediately after development and testing is completed. To ensure that applications are deployed successfully, an automatic and transparent process is required. We refer to this process as Zero Touch Deployment™. This white paper reviews two approaches to Zero Touch Deployment--a script-based solution and a release automation platform. The article discusses how each can solve the key …

  • On-demand Event Event Date: December 18, 2014 The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this webcast and join industry experts as …

Most Popular Programming Stories

More for Developers

RSS Feeds