Link to home
Start Free TrialLog in
Avatar of sjcu
sjcu

asked on

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;
                  ::GetCursorPos(&pt);
                  m_treeCtrl.ScreenToClient(&pt);
                  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            
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

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
Avatar of sjcu
sjcu

ASKER

Thanks. It works. I also need to get the pFlags.... As in my code i have a check for (uFlags & TVHT_ONITEM).
ASKER CERTIFIED SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sjcu

ASKER

Thanks for the fast response