# grabKeyboardFocus(), wantsKeyboardFocus()

**URL:** <https://forum.juce.com/t/grabkeyboardfocus-wantskeyboardfocus/47990>\
**Category:** General JUCE discussion\
**Created:** [September 26, 2021, 11:40am UTC](https://forum.juce.com/t/grabkeyboardfocus-wantskeyboardfocus/47990 "2021-09-26T11:40:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mashu](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/mashu/32/579_2.png) [@mashu](https://forum.juce.com/u/mashu)\
**Post date:** [September 26, 2021, 11:40am UTC](https://forum.juce.com/t/grabkeyboardfocus-wantskeyboardfocus/47990/1 "2021-09-26T11:40:04Z")

</div>

Hi, this has had me stumped for the last couple of days.

Here’s what I want to achieve:

I have a parent component as a “canvas” on which child components can be placed. If I dynamically create a new child component, it should be brought to the front, and given keyboard focus. The component with keyboard focus needs to be highlighted (with for example a red rectangle, in its paint() method). Clicking on child components should also bring them to the front and give them keyboard focus and also highlight them.

I have tried so many things, but have failed to get it to work properly yet. It often seems that I need to click twice, or actually move the child components to get them to respond properly.

I have tried so many different things with focusChangedListener, setWantsKeyboardFocus(), grabKeyboardFocus; explicitly setting focus order, setting timers to check that components are showing…etc  
But almost always seem to need to click twice or move the child components before the highlight gets drawn on the correct component, and that component receives keyboard focus.

---

<div class="post-metadata">

**Author:** ![eyalamir](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/eyalamir/32/2978_2.png) [@eyalamir](https://forum.juce.com/u/eyalamir)\
**Post date:** [September 26, 2021, 2:32pm UTC](https://forum.juce.com/t/grabkeyboardfocus-wantskeyboardfocus/47990/2 "2021-09-26T14:32:16Z")

</div>

I’ve implemented an example of my understanding what you described here:

> **[JUCE-Examples/Apps/DynamicComponents at master · eyalamirmusic/JUCE-Examples](https://github.com/eyalamirmusic/JUCE-Examples/tree/master/Apps/DynamicComponents)**
>
> master/Apps/DynamicComponents

---

<div class="post-metadata">

**Author:** ![mashu](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/mashu/32/579_2.png) [@mashu](https://forum.juce.com/u/mashu)\
**Post date:** [September 26, 2021, 3:55pm UTC](https://forum.juce.com/t/grabkeyboardfocus-wantskeyboardfocus/47990/3 "2021-09-26T15:55:20Z")

</div>

Thank you very much for taking the time to do that [eyalamir](https://forum.juce.com/u/eyalamir), It works almost perfectly.

I now have it working in all except ONE scenario: I want to create widgets by dragging and dropping files onto the canvas. It seems when I dynamically create widgets that way, the keyboard focus is still given to the previous widget.

From this small change to your canvas component to enable file drag and drop to create widgets, you will see what i mean:  
[Canvas.h](https://forum.juce.com/uploads/short-url/kBsZnJe9KbKewnnT3fascyVEYzd.h) (1.1 KB)

My guess is that the whole app window goes out of focus when drag and drop occurs, and this in turn messes with the keyboard focus.

---

<div class="post-metadata">

**Author:** ![eyalamir](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/eyalamir/32/2978_2.png) [@eyalamir](https://forum.juce.com/u/eyalamir)\
**Post date:** [September 26, 2021, 4:01pm UTC](https://forum.juce.com/t/grabkeyboardfocus-wantskeyboardfocus/47990/4 "2021-09-26T16:01:47Z")

</div>

Yes, that’s the OS getting the app out of focus, I don’t think it’s something can be done in JUCE (but maybe I’m wrong).

---

<div class="post-metadata">

**Author:** ![eyalamir](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/eyalamir/32/2978_2.png) [@eyalamir](https://forum.juce.com/u/eyalamir)\
**Post date:** [September 26, 2021, 4:02pm UTC](https://forum.juce.com/t/grabkeyboardfocus-wantskeyboardfocus/47990/5 "2021-09-26T16:02:20Z")

</div>

You probably can store the highlighted component and on a timer bring the focus back.

---

<div class="post-metadata">

**Author:** ![mashu](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/mashu/32/579_2.png) [@mashu](https://forum.juce.com/u/mashu)\
**Post date:** [September 28, 2021, 4:54am UTC](https://forum.juce.com/t/grabkeyboardfocus-wantskeyboardfocus/47990/6 "2021-09-28T04:54:45Z")

</div>

Thank you again. Have got it all working now (I Hope!)

Had to use a timer in my widgets to check the focus bool against the actual hasKeyboardFocus() result.

```auto
void Widget::timerCallback()
{
    if (widgetFocus && !hasKeyboardFocus(true))
    {
        grabKeyboardFocus();
        if (hasKeyboardFocus(true))
        {
            stopTimer();
        }
    }
}

```
