Windows: first click into an inactive window focuses a TextEditor, but keys go elsewhere (since 8.0.9)

Hi,

since JUCE 8.0.9 I’m seeing this on Windows: when my app’s window is not active and I click straight into a TextEditor, the window activates and the caret appears, but typing does nothing. A second click on the editor fixes it. If I first click somewhere else in the window and then into the editor, it works right away. Same thing for every TextEditor in the app, so it’s not component specific.

I think I’ve tracked it down. Up to 8.0.8, WM_SETFOCUS was handled on the spot, so ComponentPeer::handleFocusGain() restored the previously focused component before the click arrived, and the click then moved focus to the editor the normal way. Commit 2d243486c (“Windows: Fix bug where mouse events would not reach background windows blocked by modal windows”, June 2025, first shipped in 8.0.9) changed that for a good reason: with a modal foreground window open, calling SetFocus from inside the WM_SETFOCUS handler made Windows drop the pending mouse event for the clicked window, which broke dragging toolbar items behind the customisation dialog in the demo. So the focus handling is now deferred until after the activating mouse event (the mouseActivateFlags logic in juce_Windowing_windows.cpp). The order is now:

  1. WM_LBUTTONDOWN reaches the TextEditor, which grabs keyboard focus and shows its caret.
  2. Right after that, doMouseDown() runs the postponed handleSetFocus() → ComponentPeer::handleFocusGain().
  3. handleFocusGain() assumes nothing has focus yet and restores lastFocusedComponent (whatever had focus when the window was deactivated) by assigning currentlyFocusedComponent directly. The TextEditor never gets a focus-lost callback, so it keeps its caret, but the key events now go to the old component.

That also explains why it doesn’t happen every time: if the last focused component happens to be the editor you click, or there was none, the restore is harmless.

A small check at the top of handleFocusGain() fixes it for me and keeps the deferral, so the toolbar case stays fixed. If the focus already sits inside this window, there’s nothing to restore:

--- a/modules/juce_gui_basics/windows/juce_ComponentPeer.cpp

+++ b/modules/juce_gui_basics/windows/juce_ComponentPeer.cpp

@@ -361,6 +361,11 @@ void ComponentPeer::handleMovedOrResized()

 void ComponentPeer::handleFocusGain()
 {
+    // Focus already sits inside this window (e.g. a mouse event processed before a
+    // deferred WM_SETFOCUS gave it to a child component), so leave it there.
+    if (component.isParentOf (Component::currentlyFocusedComponent))
+        return;
+
     if (component.isParentOf (lastFocusedComponent)
           && lastFocusedComponent->isShowing()
           && lastFocusedComponent->getWantsKeyboardFocus())

The programmatic path is unaffected, since takeKeyboardFocus() calls peer->grabFocus() before it assigns currentlyFocusedComponent. Other platforms don’t defer the focus message, so currentlyFocusedComponent should always be null at that point and the check is a no-op there.

The code on the current develop tip is the same. I’m applying this as a local patch on top of develop for now, would be great to see it fixed soon.

Cheers,
Michael
juce-handleFocusGain.patch (804 Bytes)

1 Like

Thank you for reporting. A fix is out on develop.

1 Like

Thanks