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

  • Protecting business operations means shifting the priorities around availability from disaster recovery to business continuity. Enterprises are shifting their focus from recovery from a disaster to preventing the disaster in the first place. With this change in mindset, disaster recovery is no longer the first line of defense; the organizations with a smarter business continuity practice are less impacted when disasters strike. This SmartSelect will provide insight to help guide your enterprise toward better …

  • Savvy enterprises are discovering that the cloud holds the power to transform IT processes and support business objectives. IT departments can use the cloud to redefine the continuum of development and operations—a process that is becoming known as DevOps. Download the Executive Brief DevOps: Why IT Operations Managers Should Care About the Cloud—prepared by Frost & Sullivan and sponsored by IBM—to learn how IBM SmartCloud Application services provide a robust platform that streamlines …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds