4
votes

I am trying to detect in my application, if the Enter/Return buttons are pressed. My problem is that the LVN_KEYDOWN event (Indicates that a key has been pressed) does not detect the Enter/Return key.

I have seen similar questions for other languages, but can not find a solution for C++.

My event to read the key press is:

void ListOption::OnLvnKeydownList1(NMHDR *pNMHDR, LRESULT *pResult)
{
    LPNMLVKEYDOWN pLVKeyDow = reinterpret_cast<LPNMLVKEYDOWN>(pNMHDR);
    // TODO: Add your control notification handler code here
    if(pLVKeyDow->wVKey == VK_RETURN)
    {
        OnItemActivateList1(pNMHDR, pResult);
        *pResult = 1;
    }
    *pResult = 0;
}

This code works for almost any key, execept for the Enter key.

My dialog has only one button, and it's "Default Button" value is FALSE. How is it possible to detect the keypress?

Update: My application uses modal dialogs.. It contains a CImageSheet that contains CImagePages(tabs). Here is an image to explain better (I have placed grey blocks to hide some private data).
enter image description here

When I press Enter, I wish to open a new dialog to change the option. Currently this is done with the LVN_ITEMCTIVATE event (when the user double clicks an item):

1
you need to provide more context about this, because I've tested this on a CListView it does work. please add the class declaration, message map etc... maybe, try to reproduce your problem in a simple application creating a new project with minimal feturesRobson
You may need to respond to WM_GETDLGCODE messages and return the appropriate flags.Mark Ransom
@Robson I have updated the question to explain a bit better. Please note I am using MFC's List Control, not CListView.Kyle Williamson
@TheForestAndTheTrees Thank you, but this is a different question. also, the question you linked to has been closed for being [too broad].Kyle Williamson

1 Answers

3
votes

You can override PreTranslateMessage in the window which owns the ListView. In this case it seems to be a CPropertyPage.

BOOL CMyPropertyPage::PreTranslateMessage(MSG* pMsg)
{
    //optional: you can handle keys only when ListView has focus
    if (GetFocus() == &List) 
    if (pMsg->message == WM_KEYDOWN)
    {
      if (pMsg->wParam == VK_RETURN)
      {
         //return 1 to eat the message, or allow for default processing
         int sel = List.GetNextItem(-1, LVNI_SELECTED);
         if (sel >= 0)
         {
            MessageBox("VK_RETURN");
            TRACE("ListView_GetNextItem %d\n", sel);
            return 1;
         }
         else
            TRACE("ListView_GetNextItem not-selected, %d\n", sel);
      }

      if (pMsg->wParam == VK_ESCAPE)
      {
         //do nothing!
      }
    }

    return CPropertyPage::PreTranslateMessage(pMsg);
}