# Keyboard Focus issue

**URL:** <https://forum.juce.com/t/keyboard-focus-issue/19406>\
**Category:** General JUCE discussion\
**Created:** [October 8, 2016, 12:42pm UTC](https://forum.juce.com/t/keyboard-focus-issue/19406 "2016-10-08T12:42:13Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![daveclissold](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daveclissold/32/1310_2.png) [@daveclissold](https://forum.juce.com/u/daveclissold)\
**Post date:** [October 8, 2016, 12:42pm UTC](https://forum.juce.com/t/keyboard-focus-issue/19406/1 "2016-10-08T12:42:13Z")

</div>

We are having problems with keyboard focus when a 3rd party hosted plugin’s GUI is focused. We have tried setting the Plugin editor requires keyboard focus to false and have tried to implement this independently with the following

```
PluginWindow::~PluginWindow()
{
    if (isActiveWindow()) {
        setWantsKeyboardFocus(false);
        DocumentWindow::setWantsKeyboardFocus(false);
        }
    }
}

```

and varied this to

```
PluginWindow::~PluginWindow()
{
    activePluginWindows.removeFirstMatchingValue (this);
    setWantsKeyboardFocus(false);
    DocumentWindow::setWantsKeyboardFocus(false);
    clearContentComponent();
    
}

```

We have also tried

```
PluginWindow::PluginWindow (Component* const uiComp,
                            AudioProcessor * owner_,
                            const bool isGeneric_)
: DocumentWindow (uiComp->getName(), Colours::lightblue,
                  DocumentWindow::minimiseButton | DocumentWindow::closeButton | DocumentWindow::setWantsKeyboardFocus(false);),

```

and tried adding

```
isGeneric (isGeneric_)
{
        uiComp->setWantsKeyboardFocus(false);
}

```

But none of these have worked, the focused window still steals keyboard focus, which is preventing Spacebar playback in the DAW whilst the 3rd party plugin GUI is focused.

---

<div class="post-metadata">

**Author:** ![t0m](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/t0m/32/16413_2.png) [@t0m](https://forum.juce.com/u/t0m)\
**Post date:** [October 12, 2016, 8:22am UTC](https://forum.juce.com/t/keyboard-focus-issue/19406/2 "2016-10-12T08:22:16Z")

</div>

The plugin’s window is free to call OS functions directly to give itself focus, and as a host there’s not much you can do about it.

One solution is to always capture spacebar presses, and dispatch them to subcomponents are required.

---

<div class="post-metadata">

**Author:** ![hungj](https://avatars.discourse-cdn.com/v4/letter/h/5f9b8f/32.png) [@hungj](https://forum.juce.com/u/hungj)\
**Post date:** [October 15, 2016, 3:19pm UTC](https://forum.juce.com/t/keyboard-focus-issue/19406/3 "2016-10-15T15:19:38Z")

</div>

Hi Tom,

Please help us how to dispatch them to subcomponents?  
Is there a way to send a space key to parent components?

Thanks

---

<div class="post-metadata">

**Author:** ![t0m](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/t0m/32/16413_2.png) [@t0m](https://forum.juce.com/u/t0m)\
**Post date:** [October 17, 2016, 8:41am UTC](https://forum.juce.com/t/keyboard-focus-issue/19406/4 "2016-10-17T08:41:09Z")

</div>

Something like this:

```
class TestWindow : public DocumentWindow
{
public:
    TestWindow ()
        : DocumentWindow ("test", Colours::red, 0)
    {}

    bool keyPressed (const KeyPress& key) override
    {
        DBG ("Key pressed in window!");
        return false;
    }
};

class MainContentComponent : public Component,
                             public KeyListener
{
public:
    //==============================================================================
    MainContentComponent()
    {
        setSize (600, 400);

        testWindow.setVisible (true);

        testWindow.addKeyListener (this);
    }

    ~MainContentComponent() {}

    void paint (Graphics& g) override {}

    void resized() override {}

    bool keyPressed (const KeyPress& key, Component* originatingComponent) override
    {
        DBG ("Caught key pressed!");
        return false;
    }

private:
    //==============================================================================
    TestWindow testWindow;

    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainContentComponent)
};

```

The return value of keyPressed indicates if you are going to consume the key press there or pass it along to the next component.

---

<div class="post-metadata">

**Author:** ![hungj](https://avatars.discourse-cdn.com/v4/letter/h/5f9b8f/32.png) [@hungj](https://forum.juce.com/u/hungj)\
**Post date:** [October 17, 2016, 3:15pm UTC](https://forum.juce.com/t/keyboard-focus-issue/19406/5 "2016-10-17T15:15:53Z")

</div>

Thanks for your answer.  
I am trying your proposal now. and keep you updated.  
Additional question:  
When i press space bar on the main plugin window, i dont see the plugin handle the key pressed handler.  
I just need to play track on DAWs.  
Is there a way to do that ?

---

<div class="post-metadata">

**Author:** ![hungj](https://avatars.discourse-cdn.com/v4/letter/h/5f9b8f/32.png) [@hungj](https://forum.juce.com/u/hungj)\
**Post date:** [October 19, 2016, 5:27pm UTC](https://forum.juce.com/t/keyboard-focus-issue/19406/6 "2016-10-19T17:27:21Z")

</div>

Thanks,

I can hook the key from the document window.  
But i can’t send it to daw to play the track.

I tried 🙂  
Return false on keypress event. But no result.

I need to send space key i hook above and send it to daw.

Now i dont know how to send space key to Daw.  
Please give me more idea  
thanks.

---

<div class="post-metadata">

**Author:** ![t0m](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/t0m/32/16413_2.png) [@t0m](https://forum.juce.com/u/t0m)\
**Post date:** [October 19, 2016, 6:03pm UTC](https://forum.juce.com/t/keyboard-focus-issue/19406/7 "2016-10-19T18:03:36Z")

</div>

Perhaps I misunderstood your use case.

If your application is a plug-in host then the above approach may work. If, however, you are trying to send a spacebar press to an external host from inside a plug-in GUI then I don’t know what will work; it’s the host’s responsibility to capture key presses it might be interested in.

---

<div class="post-metadata">

**Author:** ![hungj](https://avatars.discourse-cdn.com/v4/letter/h/5f9b8f/32.png) [@hungj](https://forum.juce.com/u/hungj)\
**Post date:** [October 19, 2016, 6:29pm UTC](https://forum.juce.com/t/keyboard-focus-issue/19406/8 "2016-10-19T18:29:13Z")

</div>

Thanks man.

I updated the information on attached image.

Jules can help me (hi hi hi hi i hope that).

 ![](https://us1.discourse-cdn.com/flex026/uploads/juce/original/2X/3/32498659ab1095acd4bab9f3c1d9993733eec4ce.PNG)

Thanks all guy

---

<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:** [October 20, 2016, 8:28am UTC](https://forum.juce.com/t/keyboard-focus-issue/19406/9 "2016-10-20T08:28:24Z")

</div>

> [@hungj](#):
>
> Jules can help me (hi hi hi hi i hope that).

Tom already looked into this, and like he says, it’s not something we consider to be working incorrectly. Hosts generally use some hacky tricks to spy on keypresses that happen inside your plugin window, but if you start creating your own separate windows, you can’t expect the host to pay any attention to them.

All I can suggest would be that you catch spacebar presses yourself in your floating window, and then write your own win32 code to inject space key events into your plugin’s HWND, so that host may spot them.

---

<div class="post-metadata">

**Author:** ![hungj](https://avatars.discourse-cdn.com/v4/letter/h/5f9b8f/32.png) [@hungj](https://forum.juce.com/u/hungj)\
**Post date:** [October 20, 2016, 10:55am UTC](https://forum.juce.com/t/keyboard-focus-issue/19406/10 "2016-10-20T10:55:29Z")

</div>

Yeah,

I considered your solution.  
I am still looking for easier way to do that.

Keep this question open, if noone can answer this question. I ll try your proposal.  
Thanks Jules,

---

<div class="post-metadata">

**Author:** ![hungj](https://avatars.discourse-cdn.com/v4/letter/h/5f9b8f/32.png) [@hungj](https://forum.juce.com/u/hungj)\
**Post date:** [March 18, 2017, 7:12pm UTC](https://forum.juce.com/t/keyboard-focus-issue/19406/11 "2017-03-18T19:12:35Z")

</div>

> [@hungj](#):
>
> am still looking for easier way to do that.
> 
> Keep this question open, if noone can answer this question. I ll try your proposal.Thanks Jules,

I fixed that with easy way.  
Thanks

---

<div class="post-metadata">

**Author:** ![charlifiiiii](https://avatars.discourse-cdn.com/v4/letter/c/e47774/32.png) [@charlifiiiii](https://forum.juce.com/u/charlifiiiii)\
**Post date:** [November 13, 2017, 8:35pm UTC](https://forum.juce.com/t/keyboard-focus-issue/19406/12 "2017-11-13T20:35:03Z")

</div>

Hi hungj,  
May I ask you what is the easy way you found ?  
Thanks
