# I'm not sure about the keyboard focus

**URL:** <https://forum.juce.com/t/im-not-sure-about-the-keyboard-focus/57893>\
**Category:** General JUCE discussion\
**Created:** [September 15, 2023, 12:05pm UTC](https://forum.juce.com/t/im-not-sure-about-the-keyboard-focus/57893 "2023-09-15T12:05:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mentsuyu](https://avatars.discourse-cdn.com/v4/letter/m/b487fb/32.png) [@mentsuyu](https://forum.juce.com/u/mentsuyu)\
**Post date:** [September 15, 2023, 12:05pm UTC](https://forum.juce.com/t/im-not-sure-about-the-keyboard-focus/57893/1 "2023-09-15T12:05:39Z")

</div>

I want the text editor to have keyboard focus under certain conditions within renderOpenGL().  
I put the following code in renderOpenGL()

```
if (myTextBox.isVisible() && !isPlay)
{
    myTextBox.setWantsKeyboardFocus(true);
    myTextBox.grabKeyboardFocus();
    int o = myTextBox.hasKeyboardFocus(true);
    LOG(juce::String(o));
}

```

(\*isPlay determines if playback is in progress.)

myTextBox.setVisible(true) when the screen is clicked.  
The first click does not focus, the second click does. I am having trouble understanding what the heck is going on.

Is there any way to get it to focus on the first click? Any help would be appreciated.

---

<div class="post-metadata">

**Author:** ![anthony-nicholls](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/anthony-nicholls/32/1246_2.png) [@anthony-nicholls](https://forum.juce.com/u/anthony-nicholls)\
**Post date:** [September 15, 2023, 1:02pm UTC](https://forum.juce.com/t/im-not-sure-about-the-keyboard-focus/57893/2 "2023-09-15T13:02:18Z")

</div>

It might be worth stepping through he code with a debugger to see what happens in `grabKeyboardFocus()`.

When you say…

> myTextBox.setVisible(true) when the screen is clicked.

Is it possible this is happening _after_ the call to `grabKeyboardFocus()`? if it is the component will fail to take focus while it’s not visible.

---

<div class="post-metadata">

**Author:** ![Rincewind](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/rincewind/32/10645_2.png) [@Rincewind](https://forum.juce.com/u/Rincewind)\
**Post date:** [September 15, 2023, 1:13pm UTC](https://forum.juce.com/t/im-not-sure-about-the-keyboard-focus/57893/3 "2023-09-15T13:13:21Z")

</div>

I’d brute force it and try to schedule the grabFocus call into the next cycle. you can use MessageManager::callAsync for that and just need to check if the component got deleted.

---

<div class="post-metadata">

**Author:** ![mentsuyu](https://avatars.discourse-cdn.com/v4/letter/m/b487fb/32.png) [@mentsuyu](https://forum.juce.com/u/mentsuyu)\
**Post date:** [September 16, 2023, 12:53am UTC](https://forum.juce.com/t/im-not-sure-about-the-keyboard-focus/57893/4 "2023-09-16T00:53:47Z")

</div>

Thanks for the suggestion.  
I have looked into it, but no matter how much I try to grabKeyboardFocus, it focuses to “Settings…” is focused to “Settings…”.  
I used juce::MessageManager::callAsync([this]) and the result was the same.

It monitors where the focus is at each render update and at the moment of click.

void Anime\_testAudioProcessorEditor::mouseDown(const juce::MouseEvent& e)  
{  
click\_n += 1;  
if (click\_n == 2) return;

```
juce::Component* focusedComponent = juce::Component::getCurrentlyFocusedComponent();
if (focusedComponent != nullptr)
{
    juce::String componentName = focusedComponent->getName();
    LOG(juce::String(componentName));
}
else
{
    LOG("No_Focus");
}

```

Curiously, if the second click is written to do nothing at all, the focus switches to the text editor on the second click.  
The following processing performed in the render  
myTextBox.setWantsKeyboardFocus(true);  
myTextBox.grabKeyboardFocus();  
are suddenly enabled.(\*Of course, I’m not clicking on a text editor.)

When I try to give focus to a specific component in a render, the focus is suddenly enabled when I call a method that removes awareness from OpenGL.  
I am currently aware that this is such a bug, but I continue to solicit information.

Thanks for the advice, and I’ll keep you posted.

---

<div class="post-metadata">

**Author:** ![evanberard](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/evanberard/32/14069_2.png) [@evanberard](https://forum.juce.com/u/evanberard)\
**Post date:** [September 20, 2023, 10:05am UTC](https://forum.juce.com/t/im-not-sure-about-the-keyboard-focus/57893/5 "2023-09-20T10:05:43Z")

</div>

I recently had an issue where using grabKeyboardFocus() wouldn’t work no matter how hard I tried. Turned out another component in the same space had an empty KeyPressed callback inherited from KeyListener. Deleting that fixed my keyboard focus issues
