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: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds