# Label and keyboard focus

**URL:** <https://forum.juce.com/t/label-and-keyboard-focus/3707>\
**Category:** Audio Plugins\
**Created:** [February 26, 2009, 5:21pm UTC](https://forum.juce.com/t/label-and-keyboard-focus/3707 "2009-02-26T17:21:05Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![\_a1](https://avatars.discourse-cdn.com/v4/letter/_/ecb155/32.png) [@\_a1](https://forum.juce.com/u/_a1)\
**Post date:** [February 26, 2009, 5:21pm UTC](https://forum.juce.com/t/label-and-keyboard-focus/3707/1 "2009-02-26T17:21:05Z")

</div>

Hi,

this is probably a noob question, but I am not sure how to handle this properly. The host is Pro Tools, and I have set JucePlugin\_EditorRequiresKeyboardFocus to 0.

How can I get the TextEditor to open when I click on a Label? It does work with JucePlugin\_EditorRequiresKeyboardFocus set to 1, but then the user would have to switch focus first before using any standard Pro Tools key commands.

Thanks,  
(a)

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [February 26, 2009, 5:26pm UTC](https://forum.juce.com/t/label-and-keyboard-focus/3707/2 "2009-02-26T17:26:34Z")

</div>

Yeah, all that stuff is a bit tricky. You might manage it just by calling setWantsKeyboardFocus (true) on the appropriate comps, but maybe do a search for JucePlugin\_EditorRequiresKeyboardFocus in the source code to get an idea of how fiddly it is.

---

<div class="post-metadata">

**Author:** ![\_a1](https://avatars.discourse-cdn.com/v4/letter/_/ecb155/32.png) [@\_a1](https://forum.juce.com/u/_a1)\
**Post date:** [February 26, 2009, 6:24pm UTC](https://forum.juce.com/t/label-and-keyboard-focus/3707/3 "2009-02-26T18:24:47Z")

</div>

Hmmm, not sure what is called by whom, but can setWantsKeyboardFocus () really work under Win32 when you have

```auto
void globalFocusChanged (JUCE_NAMESPACE::Component*)
{
#if ! JucePlugin_EditorRequiresKeyboardFocus
    if (hasKeyboardFocus (true))
        passFocusToHostWindow (hostWindow);
#endif
}
```

in EditorCompWrapper?

Just clueless,  
(a)

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [February 27, 2009, 11:43am UTC](https://forum.juce.com/t/label-and-keyboard-focus/3707/4 "2009-02-27T11:43:22Z")

</div>

Yeah, that’s what I mean - it’s quite a difficult balancing act to get keyboard handling to work alongside the host.

---

<div class="post-metadata">

**Author:** ![\_a1](https://avatars.discourse-cdn.com/v4/letter/_/ecb155/32.png) [@\_a1](https://forum.juce.com/u/_a1)\
**Post date:** [February 27, 2009, 2:27pm UTC](https://forum.juce.com/t/label-and-keyboard-focus/3707/5 "2009-02-27T14:27:30Z")

</div>

Is there any possibility to pass the focus manually to the host window? So that you could do that from the Editor?

Thanks,  
(a)

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [March 1, 2009, 11:16am UTC](https://forum.juce.com/t/label-and-keyboard-focus/3707/6 "2009-03-01T11:16:50Z")

</div>

You could have a go with native windowing calls and see what happens…

---

<div class="post-metadata">

**Author:** ![amygdala](https://avatars.discourse-cdn.com/v4/letter/a/e79b87/32.png) [@amygdala](https://forum.juce.com/u/amygdala)\
**Post date:** [March 5, 2009, 7:42am UTC](https://forum.juce.com/t/label-and-keyboard-focus/3707/7 "2009-03-05T07:42:11Z")

</div>

I have a problem that may have affinity to this… On MacOS X, when compiling my plugin as AU, I can’t get ANY keyboard events if I debug in AU Lab. Compiling as VST and using VSTi Host to debug, keyboard events hapen just fine.

Any hints?

- A

---

<div class="post-metadata">

**Author:** ![\_a1](https://avatars.discourse-cdn.com/v4/letter/_/ecb155/32.png) [@\_a1](https://forum.juce.com/u/_a1)\
**Post date:** [March 6, 2009, 11:44am UTC](https://forum.juce.com/t/label-and-keyboard-focus/3707/8 "2009-03-06T11:44:48Z")

</div>

Hi,

although I have been able to find a simple workaround for the problem on Windows RTAS (just don’t pass the focus on to the host window if one component is modal), I still have problems with Mac Os. Since I am not very familiar with Mac Os I would welcome any suggestions.

I am still trying to get my head around to the functionality of setWantsKeyboardFocus(). In Windows, the call to this function doesn’t matter too much, but in MacOs RTAS, setting this to false for the top level component results effectively in setting it to false for all children as well, as far as I understand.  
So, either the PluginEditor generally accepts keyboard focus, allowing its children to accept focus as well but at the same time stealing focus from the host unnecessarily with every click, or it refuses keyboard focus for all of its children.

Did I understand that correct, and if yes, are there any suggestions to solve/work around this?

I am just not a Mac guy, you know…

Thanks,  
(a)

---

<div class="post-metadata">

**Author:** ![cleepa](https://avatars.discourse-cdn.com/v4/letter/c/a6a055/32.png) [@cleepa](https://forum.juce.com/u/cleepa)\
**Post date:** [March 25, 2009, 7:46pm UTC](https://forum.juce.com/t/label-and-keyboard-focus/3707/9 "2009-03-25T19:46:42Z")

</div>

No workaround, but some more information that will hopefully help solve this.

I am experiencing the problem too. I previously had setWantsKeyboardFocus(true) called on the top level component, which allowed all subcomponents to get focus. However, I found that no keyboard events would get passed back to Pro Tools.

If I call setWantsKeyboardFocus(true) on individual components (not the top level component), it doesn’t work.

If I call setWantsKeyboardFocus(true) on the top level component, I can call setWantsKeyboardFocus(false) on subcomponents, which will prevent them receiving focus.

The text box associated with a slider is a special case. If setWantsKeyboardFocus() is called on a slider, the textbox is not affected. This is a problem for my plug-in, but ought to be easily solvable.

I’m hoping I can find a workaround for this by installing a keyboard handler and passing events back to the native window. I don’t know yet if this will be possible. Ideally, though, setWantsKeyboardFocus() would work properly…

---

<div class="post-metadata">

**Author:** ![cleepa](https://avatars.discourse-cdn.com/v4/letter/c/a6a055/32.png) [@cleepa](https://forum.juce.com/u/cleepa)\
**Post date:** [March 26, 2009, 6:59am UTC](https://forum.juce.com/t/label-and-keyboard-focus/3707/10 "2009-03-26T06:59:27Z")

</div>

Installing a keyboard handler doesn’t work… because the top level component must be able to accept focus.

More info: at least part of the problem seems to be with the top level ComponentPeer. In Component::takeKeyboardFocus (the Component object being a text editor - which does have the wantsKeyboardFocus flag set, contrary to my previous post), peer-\>grabFocus() is called, which seems to work fine. However, on the next line, peer-\>isFocused() fails, unless the top level component has the wantsKeyboardFocus flag set. I think it is failing because the isKeyWindow flag is not set. Seems like there is a discrepancy here…

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [March 26, 2009, 9:32am UTC](https://forum.juce.com/t/label-and-keyboard-focus/3707/11 "2009-03-26T09:32:20Z")

</div>

I seem to remember that the isKeyWindow method behaves a bit strangely, but couldn’t work out why - probably just a quirk of the cocoa windowing system.

---

<div class="post-metadata">

**Author:** ![cleepa](https://avatars.discourse-cdn.com/v4/letter/c/a6a055/32.png) [@cleepa](https://forum.juce.com/u/cleepa)\
**Post date:** [March 26, 2009, 4:48pm UTC](https://forum.juce.com/t/label-and-keyboard-focus/3707/12 "2009-03-26T16:48:27Z")

</div>

Any suggestions as to a workaround for this? This is the last blocking UI bug for us… I’m stuck when it comes to debugging this further - I don’t know any objective c/c++. Or is it something that’s on your radar to fix?

---

<div class="post-metadata">

**Author:** ![cleepa](https://avatars.discourse-cdn.com/v4/letter/c/a6a055/32.png) [@cleepa](https://forum.juce.com/u/cleepa)\
**Post date:** [March 26, 2009, 6:44pm UTC](https://forum.juce.com/t/label-and-keyboard-focus/3707/13 "2009-03-26T18:44:34Z")

</div>

Although isKeyWindow doesn’t seem to be behaving correctly, it isn’t even the source of the problem… I can comment out the call and the problem still happens. I am completely stuck…

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [March 28, 2009, 11:30am UTC](https://forum.juce.com/t/label-and-keyboard-focus/3707/14 "2009-03-28T11:30:38Z")

</div>

It’s on my radar, but slightly snowed under at the moment…
