Keyboard focus, buttons and plugins

I have a plugin with a bunch of buttons and a list view.

When the buttons are pushed I want the list view to lose keyboard focus, but I don’t want the buttons to gain keyboard focus (because they soak up the enter key which annoys Cubase users).

Any top suggestions?

There is the static Component::unfocusAllComponents().
You can override in your buttons the focusGained() to call that method.

I don’t know, if setWantsKeyboardFocus (false) on the buttons is sufficient, and if it’s the desired, but also worth a try…

1 Like

setWantsKeyboardFocus (false) was the start of my problems.

However if i put in my AudioProcessorEditor constructor setWantsKeyboardFocus(true) and then in the focusGained() override unfocusAllComponents() then we get the right behaviour at the expense of lots of calls to focus and unfocus everytime anything is clicked on.

InfiniteRecorderEditor::InfiniteRecorderEditor (InfiniteRecorderProcessor& p)
setWantsKeyboardFocus(true); // see focusGained override


void focusGained (FocusChangeType) override
    // back stop so clicking on anything that doesn't have keyboard focus specifically enabled
    // defocuses everything

This feels like it’s going to cause problems, but I don’t know what yet :slight_smile:

I know that feeling :wink:

Since you have to inherit the Buttons anyway, an alternative could be to ignore specifically the enter key?

bool MyButton::keyPressed (const KeyPress &key) override
    if (key == KeyPress::returnKey)
        return false;

    return Button::keyPressed (key);  // or which ever button you inherited...

Last remark, if all else fails, having a look at the KeyboardFocusTraverser, but I would be surprised, if that was necessary…

Hope that helps

Oh that’s not a bad idea. Overriding keypressed seems a lot safer :slight_smile: