Detecting return in a file browser


#1

I've got a FileTreeComponent, and I'd like to be able to detect when someone presses Return on a file so I can load it.  FileBrowserListener provides me with information on selected, double clicked and clicked files.  However I can't find any way of detecting Return being pressed - FileTreeComponent soaks it up.  I can detect other key presses. 

Any workarounds, or perhaps another callback added to FileBrowserListener .. 

cheers!


#2

Actually, if you changed: 


void TreeView::toggleOpenSelectedItem()
{
    if (TreeViewItem* const firstSelected = getSelectedItem (0))
        firstSelected->setOpen (! firstSelected->isOpen());
} 

To be


bool TreeView::toggleOpenSelectedItem()
{
    if (TreeViewItem* const firstSelected = getSelectedItem (0)) {
        if (firstSelected->getNumSubItems() == 0) return false; 
        firstSelected->setOpen (! firstSelected->isOpen());
        return true; 
    }
}

And then changed


bool TreeView::keyPressed (const KeyPress& key)
{
    if (rootItem != nullptr)
    {
        if (key == KeyPress::upKey)       { moveSelectedRow (-1); return true; }
        if (key == KeyPress::downKey)     { moveSelectedRow (1);  return true; }
        if (key == KeyPress::homeKey)     { moveSelectedRow (-0x3fffffff); return true; }
        if (key == KeyPress::endKey)      { moveSelectedRow (0x3fffffff);  return true; }
        if (key == KeyPress::pageUpKey)   { moveByPages (-1); return true; }
        if (key == KeyPress::pageDownKey) { moveByPages (1);  return true; }
        if (key == KeyPress::returnKey)   { return toggleOpenSelectedItem(); } // <<< CHANGE IS HERE
        if (key == KeyPress::leftKey)     { moveOutOfSelectedItem();  return true; }
        if (key == KeyPress::rightKey)    { moveIntoSelectedItem();   return true; }
    }
    return false;
}

Then it shouldn't catch the return key when there's no subitems to open or close, and then it can be handled by the application easily.

Trying to do it through a FileBrowserListener is giving me a head ache :)

 


#3

Excellent idea! Thanks, I've added that now (though the correct thing to use is actually mightContainSubItems(), since some items won't bother adding their sub-items until you try to open them)


#4

Awesome - thank you.  Super speedy (and glad I did something useful there!) - I'll get my return handling code sorted out.