TreeView and keyboard focus


#1

I’m having some difficulty with a standard TreeView in regards to it’s processing of keystrokes and wondered how I might solve the following problem:

I have a tall rectangular component, using a horizontal divider bar to split it into top and bottom halves.
In the top half I have a treeview with a list of song names.
In the bottom half I have a ‘properties component’ which displays info about the selected song from the top half.

if the horizontal divider bar is ‘down’ far enough that the TreeView can display all the items, and has no scrollbar, then clicking in the TreeView correctly selects a song, and the up/down arrows on the keyboard will scroll from item to item as expected.

The problem occurs when the horizontal divider bar (and layout) is moved up such that the TreeView is too small to contain all the items and the scrollbars appear. At this point the scrollbars seem to be catching the up/down keys and the keyboard is controlling the scrollbar thumb position instead of selecting tree items.

Is there any way to tell the TreeView not to let the scrollbars have the keyboard focus? I looked around in the code but it’s a wee bit complicated and I was hoping this scenario has already been dealt with and am hoping I just don’t know which spell to invoke in order to make the desired magic happen…


#2

You could probably do something like this:

myTreeView->getViewport()->getVerticalScrollBar()->setWantsKeyboardFocus(false);

Though that would disable the scrollbar from ever getting focus, unless you reset it to want keyboard focus on mouseDown events or something.

So, maybe it’s better to override the Component::focusOfChildComponentChanged() method for the Treeview and then rearrange who gets the focus from in there.


#3

Thanks Aaron,
I hadn’t noticed the wantsKeyboardFocus method (should have looked harder) but…

Unfortunately none of these had any effect!

        treeView->setWantsKeyboardFocus(false);
	treeView->getViewport()->setWantsKeyboardFocus(false);
	treeView->getViewport()->getVerticalScrollBar()->setWantsKeyboardFocus(false);
	treeView->getViewport()->getViewedComponent()->setWantsKeyboardFocus(false);

I’m moving onto your second suggestion now!
Any other magic spells come to mind?


#4

The scrollbars grab the keyboard and just don’t want to let go…Any suggestions Jules? What am I doing wrong? Or not doing? It seems kind of counter-intuitive to me that the scrollbars should think they have priority over the items in the list. Your thoughts?


#5

If you’ve called setWantsKeyboardFocus on them, they really shouldn’t be able to get focused at all! Perhaps you’re calling that after they’ve already been focused, or something?


#6

The better solution here is really the second …

… override the Component::focusOfChildComponentChanged() method for the Treeview and then rearrange who gets the focus from in there.


#7

Thanks guys, sorry I didn’t respond right away - was working on another aspect of the software…

I was making the ‘wantsKeyboardFocus’ calls in the constructor of the component (created by jucer) that contained the treeview. So I think I made the calls before the treeview could grab any focus. I still have the issue, and will probably try the second alternative today Aaron.

My code had gotten a bit messy, and I am completing a round of refactoring and stupidity removal within my source code when brings me to the follwing topic which I will address in a new post entitled ‘Modules Status’