ComboBox does not respect setWantsKeyboardFocus(false)


#1

While adding improved keyboard handling I notices that ComboBoxes still receive keyboard focus even after setWantsKeyboardFocus(false) has been called.

Is this a bug or do ComboBoxes need special treatment to not receive keyboard focus?


#2

If you give them an editable text label then they'll automatically enable keyboard focus, because obviously they need it for that.


#3

I don't think I am doing that. Here is the kind of setup code I use for a ComboBox:

m_combo.addItem("Item 1", 1);
m_combo.addItem("Item 2", 2);
m_combo.addItem("Item 3", 3);
m_combo.setSelectedItemIndex(0);
m_combo.addListener(this);
m_combo.setWantsKeyboardFocus(false);
addAndMakeVisible(&m_combo);

With that setup it still receives focus.


#4

What I do in these cases is step into “m_combo.setWantsKeyboardFocus(false);” in the debugger and set a breakpoint with the condition “this==[m_combo’s current this-pointer]”. If the Combobox’s keyboard focus is changed you’ll see from where this happens.

Of course, this won’t work if it’s actually something else like the label that gets the focus and responds to it.


#5

Thanks! This is a good way to easily track what's going on.

Everytime parentHierarchyChanged() is called on the ComboBox it calls lookAndFeelChanged() which in turn calls setWantsKeyboardFocus:

setWantsKeyboardFocus (! label->isEditable());

As far as I can see there is no way to have a ComboBox not receive focus. The parent hierarchy of a combo box changes all the time. E.g. if you add any control after the ComboBox was added.


#6

lookAndFeelChanged() is virtual; you could inherit from ComboBox and overwrite it.


#7

Right!

I would prefer to have a way to disable focus for ComboBoxes without having to subclass it though.

Also, I still wonder if the side effect of reseting setWantsKeyboardFocus in lookAndFeelChanged is intended.

Hopefully Jules can clarify.

 


#8

So there is no other way but to subclass?