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

  • With JRebel, developers get to see their code changes immediately, fine-tune their code with incremental changes, debug, explore and deploy their code with ease (both locally and remotely), and ultimately spend more time coding instead of waiting for the dreaded application redeploy to finish. Every time a developer tests a code change it takes minutes to build and deploy the application. JRebel keeps the app server running at all times, so testing is instantaneous and interactive.

  • Download the Information Governance Survey Benchmark Report to gain insights that can help you further establish business value in your Records and Information Management (RIM) program and across your entire organization. Discover how your peers in the industry are dealing with this evolving information lifecycle management environment and uncover key insights such as: 87% of organizations surveyed have a RIM program in place 8% measure compliance 64% cannot get employees to "let go" of information for …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds