• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1090
  • Last Modified:

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            
0
sjcu
Asked:
sjcu
  • 2
  • 2
1 Solution
 
AndyAinscowCommented:
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
0
 
sjcuAuthor Commented:
Thanks. It works. I also need to get the pFlags.... As in my code i have a check for (uFlags & TVHT_ONITEM).
0
 
AndyAinscowCommented:
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)
0
 
sjcuAuthor Commented:
Thanks for the fast response
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now