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

  • Live Event Date: September 16, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you starting an on-premise-to-cloud data migration project? Have you thought about how much space you might need for your online platform or how to handle data that might be related to users who no longer exist? If these questions or any other concerns have been plaguing you about your migration project, check out this eSeminar. Join our speakers Betsy Bilhorn, VP, Product Management at Scribe, Mike Virnig, PowerSucess Manager and Michele …

  • As mobile devices have pushed their way into the enterprise, they have brought cloud apps along with them. This app explosion means account passwords are multiplying, which exposes corporate data and leads to help desk calls from frustrated users. This paper will discover how IT can improve user productivity, gain visibility and control over SaaS and mobile apps, and stop password sprawl. Download this white paper to learn: How you can leverage your existing AD to manage app access. Key capabilities to …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds