Losing keyboardfocus on a component

Hello,

I have a problem with keyboardfocus, I have a component that I want to grab keyboardfocus when my mouse enters the component, if the mouse is over the component ‘bool focus’ is true, which works fine. However, this component is the only place in my entire code i use grabKeyboardFocus().

My code is on the end of this post.

With these debugging outputDebugStrings i can see that the component has focus after grabbing it, but the next cycle it has lost the keyboardfocus, this if-function gets excecuted every paintcycle. “has focus before grabbing” gets never printed, “grabbed focus” and “has focus after grabbing” always.

In the consturctor of the component i also setted: wantsKeyboardFocus(true)

Is there something in Juce that automatically grabs the keyboardfocus I don’t know about? I can’t figure out why it loses keyboardFocus after each paintcycle?

    if (!hasKeyboardFocus(true) && focus) {
        if (hasKeyboardFocus(true)) {
            juce::Logger::outputDebugString("has focus before grabbing");
        }
        grabKeyboardFocus();
        juce::Logger::outputDebugString("grabbed focus");

        if (hasKeyboardFocus(true)) {
            juce::Logger::outputDebugString("has focus after grabbing");
       }
    }```