# Keyboard focus, buttons and plugins

**URL:** <https://forum.juce.com/t/keyboard-focus-buttons-and-plugins/29295>\
**Category:** General JUCE discussion\
**Created:** [September 6, 2018, 10:13am UTC](https://forum.juce.com/t/keyboard-focus-buttons-and-plugins/29295 "2018-09-06T10:13:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jimc](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/jimc/32/16067_2.png) [@jimc](https://forum.juce.com/u/jimc)\
**Post date:** [September 6, 2018, 10:13am UTC](https://forum.juce.com/t/keyboard-focus-buttons-and-plugins/29295/1 "2018-09-06T10:13:01Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [September 6, 2018, 10:42am UTC](https://forum.juce.com/t/keyboard-focus-buttons-and-plugins/29295/2 "2018-09-06T10:42:45Z")

</div>

There is the static [Component::unfocusAllComponents()](https://docs.juce.com/develop/classComponent.html#a34c592c531e8c996dee1bffb2884ca4e).  
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…

---

<div class="post-metadata">

**Author:** ![jimc](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/jimc/32/16067_2.png) [@jimc](https://forum.juce.com/u/jimc)\
**Post date:** [September 6, 2018, 10:45am UTC](https://forum.juce.com/t/keyboard-focus-buttons-and-plugins/29295/3 "2018-09-06T10:45:25Z")

</div>

`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)
:
AudioProcessorEditor(&p),
processor(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
    unfocusAllComponents();
}
```

---

<div class="post-metadata">

**Author:** ![jimc](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/jimc/32/16067_2.png) [@jimc](https://forum.juce.com/u/jimc)\
**Post date:** [September 6, 2018, 10:48am UTC](https://forum.juce.com/t/keyboard-focus-buttons-and-plugins/29295/4 "2018-09-06T10:48:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [September 6, 2018, 10:58am UTC](https://forum.juce.com/t/keyboard-focus-buttons-and-plugins/29295/5 "2018-09-06T10:58:32Z")

</div>

> [@jimc](#):
>
> This feels like it’s going to cause problems, but I don’t know what yet 🙂

I know that feeling 😉

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

```auto
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](https://docs.juce.com/develop/classKeyboardFocusTraverser.html), but I would be surprised, if that was necessary…

Hope that helps

---

<div class="post-metadata">

**Author:** ![jimc](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/jimc/32/16067_2.png) [@jimc](https://forum.juce.com/u/jimc)\
**Post date:** [September 6, 2018, 11:17am UTC](https://forum.juce.com/t/keyboard-focus-buttons-and-plugins/29295/6 "2018-09-06T11:17:58Z")

</div>

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