ListBox focus problem


#1

I’m having a slight issue with keyboard focus within a ListBox.

Certiain rows on my list require the keyboard focus when they are selected. Imagine, for example, a slider on a row component that can be adjusted with left-right when the row is selected. In order to achieve this, the row component must grab the focus when it is selected. This works.

However, if the list is resized such that the viewport’s scrollbar becomes visible, it recieves the passed-out keypresses (ignored by the row component) instead of the listbox.

So, if a scrollbar is visible and i click on the list, i get one working up/down press (changing the row, and then giving focus to the new one) before the scrollbar recieves them. If that one step takes me to a row with a slider on it, the left/right works, but up/down control the scrollbar instead of row selection.

I guess I could get around it by specifically forwarding appropriate keys to the list component from the row component, but that’s not particularly elegant.

Any ideas on an alternative solution?


#2

Well the correct thing to do is just to return true from your keyPressed method to stop the keys being passed-on. Doesn’t the slider consume those presses?


#3

My row component’s keypressed function is returning true for left/right keys, but the up/down keys are supposed to make it back to the ListBox - so i return false for other keys right? That’s what i’m doing anyway. It’s fine when there’s no scrollbar - the listbox gets the up/downs and what have you. But when a scrollbar is there, the keypresses get sent to that instead of the listbox.


#4

I see… interesting. Looks like the listbox needs to override that behaviour to stop the viewport sending the keys to its scrollbar. I’ve checked-in a new ListBox.cpp that you might want to try out.


#5

Perfect! Thanks a lot!


#6