Menu

Detect Enter key in a ListView

2003-05-02
2012-09-26
  • Nobody/Anonymous

    In my program I have a file browser made from a list view.  I can detect mouse double-clicks and key presses -- except for the Enter key.  I'm switching on ((LPNMHDR)lParam)->code and have tried LVN_KEYDOWN (looking at wVKey and hdr.code) and NM_RETURN, but have been unsuccessful thus far.  Any help would be appreciated.  Thanks.

    Windows XP with DevCpp 4.9.8.0

    Phillip

     
    • Nobody/Anonymous

      A listview control sends a WM_NOTIFY to the parent of the list view ... so I presume you are switching in the parent window on the WM_NOTIFY and then inside the WM_NOTIFY, you are switching on LVN_KEYDOWN.

      case WM_NOTIFY :
         switch (((LPNMHDR)lParam)->code)
         {
            case LVN_KEYDOWN :
                  .
                  .
                  .

      Questions :

      (1) do you get a WM_NOTIFY message when you press any key (not just the Enter key)?

      (2) do you get a LVN_KEYDOWN as part of the WM_NOTIFY message?

      Personally, I would suspect that your list view control does not have keyboard focus, consequently it cannot respond to a key press.

      If your list view is part of a dialog box, you can try sending the dialog box a DM_SETDEFID message along with the ID of your listview control to make the list view have focus (ok, I'm not 100% sure about the message, since I don't have access to my code, but reading the Win32 programmer's reference seems to indicate this is correct)

      rr

       
    • Nobody/Anonymous

      LRESULT CALLBACK WndProc( HWND hwnd, UINT Msg, WPARAM wParam, LPARAM lParam )
      {
          switch( Msg )
          {
      ...
              case WM_NOTIFY:
              {
                  switch( ((LPNMHDR)lParam)->idFrom )
                  {
                      case IDC_FILESELECT:
                      {
                          switch( ((LPNMHDR)lParam)->code )
                          {
                              case LVN_KEYDOWN:
                              {
                                  char Temp[10];
                                  sprintf( Temp, "%d %c", ((LV_KEYDOWN FAR *)lParam)->wVKey, ((LV_KEYDOWN FAR *)lParam)->wVKey ) ;
                                  MessageBox( NULL, Temp, "Key pressed", MB_OK );
                                  if( ((LV_KEYDOWN FAR *)lParam)->wVKey == VK_RETURN )
                                  /* TODO (#1#): Detect <ENTER> */
                                  {
                                      MessageBox( NULL, "You pressed ENTER", "boo", MB_OK );
                                  }
                              }
                              break;
                              case NM_RETURN:
                              {
                                  MessageBox( NULL, "work you stupid thing", "blah", MB_OK );
                              }
                              break;
                              case NM_DBLCLK:
                              {
      ...

      LVN_KEYDOWN will show pretty much any keypress besides Enter, and NM_DBLCLICK works.
      So to answer your questions, 1) yes and 2) yes except for Enter

       
    • Nobody/Anonymous

      For anyone that's interested, I finally figured this out.
      Inside my GetMessage loop, I was using IsDialogMessage to process tabs, etc....well, it also captures Enter.  Here is some code to work around this (I'm checking for Enter, but the same applies for Tab and the arrow keys).

          while( GetMessage( &Msg, NULL, 0, 0 ) > 0 )
          {
              if( ( LOWORD( Msg.message ) == WM_KEYDOWN && Msg.hwnd == GetDlgItem( hwnd, IDC_FILESELECT )
              && Msg.wParam == VK_RETURN ) || !IsDialogMessage( hwnd, &Msg ) )
              {
                  TranslateMessage( &Msg );
                  DispatchMessage( &Msg );
              }
          }

      Hope this helps save someone from the frustration I've been going through.

       
    • Nobody/Anonymous

      WM_DLGCODE

      The WM_GETDLGCODE message is sent to the dialog box procedure associated with a control. Normally, Windows handles all arrow-key and TAB-key input to the control. By responding to the WM_GETDLGCODE message, an application can take control of a particular type of input and process the input itself.

      WM_GETDLGCODE 

      Parameters

      This message has no parameters.

      Return Values

      The return value is one or more of the following values, indicating which type of input the application processes.

      Value    Meaning
      DLGC_BUTTON    Button.
      DLGC_DEFPUSHBUTTON    Default push button.
      DLGC_HASSETSEL    EM_SETSEL messages.
      DLGC_RADIOBUTTON    Radio button.
      DLGC_STATIC    Static control.
      DLGC_UNDEFPUSHBUTTON    Nondefault push button.
      DLGC_WANTALLKEYS    All keyboard input.
      DLGC_WANTARROWS    Direction keys.
      DLGC_WANTCHARS    WM_CHAR messages.
      DLGC_WANTMESSAGE    All keyboard input (the application passes this message on to a control).
      DLGC_WANTTAB    TAB key.

      Default Action

      The DefWindowProc function returns zero.

      Remarks

      Although the DefWindowProc function always returns zero in response to the WM_GETDLGCODE message, the window procedure for the predefined control classes return a code appropriate for each class.
      The WM_GETDLGCODE message and the returned values are useful only with user-defined dialog box controls or standard controls modified by subclassing.

      From the Win32Api.hlp

      rr

       
    • Nobody/Anonymous

      This weekend I found I was able to trap the enter key for a single line edit control in a dialog by using the DM_SETDEFID message to set the default ID to be my edit control instead of the OK button (which I didn't have).

      rr

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.