From 14e25d505d88cb11745a93b96c23e320e6cd2cc2 Mon Sep 17 00:00:00 2001 From: Volker Hilsheimer Date: Wed, 16 Jun 2021 15:37:05 +0200 Subject: [PATCH] macOS: Resend event if the first input method event changes focus MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit QAbstractItemView opens the editor and changes focus in response to the first QEvent::InputMethod event. Changing focus however cancels the current composition, so the key press is lost and needs to be repeated by the user. What QAbstractItemView does is probably not that unique, so fixing this there - if possible at all - is not a general solution. In the platform specific input method handling code, we can generally discover that the focus object changed in response to the InputMethod event that gets sent to the focus object. On macOS, we can then pass the key event to the input method once more, which works fine. On Windows and X11, the situation is more complicated. That the focus changed is easily discovered, but re-sending the key press in such a way that the input method responds doesn't seem possible. On Windows, using SendInput simply delivers the exact key to the focus object, without starting the expected composition. On X11 with IBus, it should be possible to call ProcessKeyEvent again on the IBus input context when we discover in filterEventFinished that the focus object changed while delivering the event. However, any attempt to do so has no effect. So, for now only fixing this for macOS. Task-number: QTBUG-54848 Change-Id: I0f7b786cfa57430e6a1e7d605415265cf53eb675 Reviewed-by: Tor Arne Vestbø (cherry picked from commit 0c2ca26cd9583092f3ade7b18bc2423336a6202b) Reviewed-by: Volker Hilsheimer --- src/plugins/platforms/cocoa/qnsview_keys.mm | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/plugins/platforms/cocoa/qnsview_keys.mm b/src/plugins/platforms/cocoa/qnsview_keys.mm index d58155237e0..57f7fe1fdd9 100644 --- a/src/plugins/platforms/cocoa/qnsview_keys.mm +++ b/src/plugins/platforms/cocoa/qnsview_keys.mm @@ -117,6 +117,11 @@ // pass the key event to the input method. note that m_sendKeyEvent may be set to false during this call m_currentlyInterpretedKeyEvent = nsevent; [self interpretKeyEvents:@[nsevent]]; + // If the receiver opens an editor in response to a key press, then the focus will change, the input + // method will be reset, and the first key press will be gone. If the focus object changes, then we + // need to pass the key event to the input method once more. + if (qApp->focusObject() != fo) + [self interpretKeyEvents:@[nsevent]]; m_currentlyInterpretedKeyEvent = 0; // if the last key we sent was dead, then pass the next key to the IM as well to complete composition m_lastKeyDead = text.isEmpty();