Handling ENter event on LIst Ctrl

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a list control. On doubleclick of list item, a new dlg opens. Now, i want to handle 'OnEnter Key'also. i.e. When a list item is selected & Enter key is pressed, the new dlg shd open
I tried implementing with NM_RETURN. But, the application does not catch this msg
Also i tried with LVN_KEYDOWN.
void CListExDlg::OnKeydownList(NMHDR* pNMHDR, LRESULT* pResult)

LV_KEYDOWN* pLVKeyDow = (LV_KEYDOWN*)pNMHDR
// TODO: Add your control notification handler code her

if(pLVKeyDow->wVKey == VK_RETURN

AfxMessageBox("KeyDOwn");

els

AfxMessageBox("NO");

*pResult = 0


But it does not catch 'Enter' Key of KeyBoard.
But when if(pLVKeyDow->wVKey == VK_SHIFT), this works fine

Please let me know how 'Enter' Key event can be checked
 
I have a list control. On doubleclick of list item, a new dlg opens. Now, i want to handle 'OnEnter Key'also. i.e. When a list item is selected & Enter key is pressed, the new dlg shd open.
I tried implementing with NM_RETURN. But, the application does not catch this msg.
Also i tried with LVN_KEYDOWN.
But it does not catch 'Enter' Key of KeyBoard.
But when if(pLVKeyDow->wVKey == VK_SHIFT), this works fine.

Please let me know how 'Enter' Key event can be checked

Is your listview control on a dialog? If it is, the dialog's OnOK
(WM_COMMAND IDOK) handler will be getting called since Enter is the
default operation. One possibility would be to try handling the
WM_KEYDOWN message in the dialog's PreTranslateMessage handler and
detect focus is on your listview control.

Dave
 
Dave is right, excep

When someone hits ENTER in a list box the return value is offically the default button of the
list box. This can be anything from IDOK to IDYES to IDCANCEL to ID_etc

If there is NO default button in a list box, then the return value of ENTER in a Dialog list box is IDOK

The VK_RETURN is not used in a list box..

There are other ways of getting ENTER from a Dialog list box, none of which are as
simple as

HWND hwnd
HWND hwndListBox

switch (wParam

case IDOK:hwnd=GetFocus()
if (hwnd == handListBox

MessageBox(NULL,"Someone hit ENTER","ListBox",MB_OK)

els

MessageBox(NULL,"No one hit ENTER","ListBox",MB_OK)

default:break
}

James Sexto
 
When someone hits ENTER in a list box the return value is offically the default button of the
list box.

James,

Could you explain what you're referring to here. I'm not aware of any
such facility in a list box control.

Dave
 
I did try Overriding PreTranslate Msg
I checked whether the focus is on the ListCtrl. If yes, then if the list item is selected & 'enter' of keybrd is entered, then, it calls a new dlg. Though this code seemed ok, now i face a new problem. When enter clicked & new dlg opened, if the application is kept in that state itself and 'ALT TAB' is clicked and focus moves to some other application, My APplication crashes. The same problem does not crash when PreTranslate msg is not overridden. Any guesses

-Niks
 
I did try Overriding PreTranslate Msg.
I checked whether the focus is on the ListCtrl. If yes, then if the list item is selected & 'enter' of keybrd is entered, then, it calls a new dlg. Though this code seemed ok, now i face a new problem. When enter clicked & new dlg opened, if the application is kept in that state itself and 'ALT TAB' is clicked and focus moves to some other application, My APplication crashes. The same problem does not crash when PreTranslate msg is not overridden. Any guesses?

I wouldn't recommend displaying another window (dialog) from
PreTranslateMessage - either use another method (handle OnOK) or have
the code in PreTranslateMessage post a custom message and display the
dialog in the custom message handler. This technique defers displaying
the dialog so its not done in the PTM hook code.

Dave
 
Back
Top