How to handle KEY_DOWN and VK_SPACE bar messages in a TreeCntrl

I have a tree control in a dialog with check boxes. I am handling NM_CLICK when the
      checkbox is changed. Now i need to be able to support accesiblitiy as well, so i have
      to handle TVN_KEYDOWN messages to capture VK_SPACE and VK_RETURN.
      The problem that i have is, i am not able to get a valid HTREEITEM.  How to
      get the valid one ?

            LPNMTVKEYDOWN pTVKeyDown = reinterpret_cast<LPNMTVKEYDOWN>(pNMHDR);
            if( ( pTVKeyDown->wVKey == VK_RETURN ) ||
             ( pTVKeyDown->wVKey == VK_SPACE  ))
                  POINT pt;
                  UINT uFlags = 0;
                  HTREEITEM hItemNew = m_treeCtrl.HitTest(pt,&uFlags);
                  m_Isdirty = TRUE;
                  OnClickAgentTree(pNMHDR, pResult);        

This code however does not work.... Any thoughts on this one ....

Thanks in advance            
Who is Participating?
AndyAinscowConnect With a Mentor Freelance programmer / ConsultantCommented:
If you are using the keyboard then the check for TVHT_ONITEM is not required (or do I misunderstand what you are wanting to achieve with the keyboard support)
AndyAinscowFreelance programmer / ConsultantCommented:
There is no guarantee the mouse cursor is where the focus is so I would avoid the GetCursorPos.

You can just query the tree for what item has the selection as this code snippet shows
      HTREEITEM hItem = GetSelectedItem();
      if(hItem == NULL)
            return NULL;      //Nothing selected
sjcuAuthor Commented:
Thanks. It works. I also need to get the pFlags.... As in my code i have a check for (uFlags & TVHT_ONITEM).
sjcuAuthor Commented:
Thanks for the fast response
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.