summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorTor Arne Vestbø <tor.arne.vestbo@qt.io>2021-08-27 12:58:25 +0200
committerTor Arne Vestbø <tor.arne.vestbo@qt.io>2021-08-27 19:49:55 +0200
commitf97e9360496eafe0425897b4aa07028965584cba (patch)
tree5caa000e29bf5bfd26bd365815d4f332f6962ded /src
parentf563203f60f2cb4d03650af3a357e988aa6bce32 (diff)
macOS: Handle IM insertNewline by sending synthetic Qt::Key_Return
Depending on the input method, pressing enter may result in simply dismissing the input method editor, without confirming the composition. For example with Hiragana, pressing 'a', then 'Tab', and then 'Enter'. In other cases it may confirm the composition as well, for example with Hiragana, pressing 'a' and then 'Enter'. And in some cases the IME will produce an explicit new line, for example with US English, pressing '~' and then 'Enter', or with 2-Set Korean, pressing 'a', then 'j', and then 'Enter'. Semantically, the input method has then asked us to insert a newline, and we should probably do so via an QInputMethodEvent, either directly or via [self insertText:@"\r"]. This is also how NSTextView handles the command. But, if we did, we would bypass all the code in Qt (and clients) that assume that pressing the return key results in a key event, for example the QLineEdit::returnPressed logic. To ensure that clients will still see the Qt::Key_Return key event, we send it as a normal key event. But, we can not fall back to handleKeyEvent for this, as the original key event may have text that reflects the combination of the inserted text and the newline, e.g. "~\r". We have already inserted the composition, so we need to follow up with a single newline event. Task-number: QTBUG-39125 Pick-to: 6.2 Change-Id: If1e95687e6d5b06011692175a325f457b0b8a72f Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Diffstat (limited to 'src')
-rw-r--r--src/plugins/platforms/cocoa/qnsview.mm2
-rw-r--r--src/plugins/platforms/cocoa/qnsview_complextext.mm34
-rw-r--r--src/plugins/platforms/cocoa/qnsview_keys.mm4
3 files changed, 32 insertions, 8 deletions
diff --git a/src/plugins/platforms/cocoa/qnsview.mm b/src/plugins/platforms/cocoa/qnsview.mm
index c1c11d5629..7de8da2da6 100644
--- a/src/plugins/platforms/cocoa/qnsview.mm
+++ b/src/plugins/platforms/cocoa/qnsview.mm
@@ -135,7 +135,6 @@ QT_NAMESPACE_ALIAS_OBJC_CLASS(QNSViewMouseMoveHelper);
// Keys
bool m_lastKeyDead;
bool m_sendKeyEvent;
- bool m_resendKeyEvent;
NSEvent *m_currentlyInterpretedKeyEvent;
QSet<quint32> m_acceptedKeyDowns;
@@ -162,7 +161,6 @@ QT_NAMESPACE_ALIAS_OBJC_CLASS(QNSViewMouseMoveHelper);
m_lastKeyDead = false;
m_sendKeyEvent = false;
- m_resendKeyEvent = false;
m_currentlyInterpretedKeyEvent = nil;
}
return self;
diff --git a/src/plugins/platforms/cocoa/qnsview_complextext.mm b/src/plugins/platforms/cocoa/qnsview_complextext.mm
index d9df96e0cd..3edbbae479 100644
--- a/src/plugins/platforms/cocoa/qnsview_complextext.mm
+++ b/src/plugins/platforms/cocoa/qnsview_complextext.mm
@@ -123,8 +123,38 @@
- (void)insertNewline:(id)sender
{
Q_UNUSED(sender);
- qCDebug(lcQpaKeys) << "Inserting newline";
- m_resendKeyEvent = true;
+
+ // Depending on the input method, pressing enter may
+ // result in simply dismissing the input method editor,
+ // without confirming the composition. In other cases
+ // it may confirm the composition as well. And in some
+ // cases the IME will produce an explicit new line, which
+ // brings us here.
+
+ // Semantically, the input method has asked us to insert
+ // a newline, and we should do so via an QInputMethodEvent,
+ // either directly or via [self insertText:@"\r"]. This is
+ // also how NSTextView handles the command. But, if we did,
+ // we would bypass all the code in Qt (and clients) that
+ // assume that pressing the return key results in a key
+ // event, for example the QLineEdit::returnPressed logic.
+ // To ensure that clients will still see the Qt::Key_Return
+ // key event, we send it as a normal key event.
+
+ // But, we can not fall back to handleKeyEvent for this,
+ // as the original key event may have text that reflects
+ // the combination of the inserted text and the newline,
+ // e.g. "~\r". We have already inserted the composition,
+ // so we need to follow up with a single newline event.
+
+ KeyEvent newlineEvent(NSApp.currentEvent);
+ newlineEvent.key = Qt::Key_Return;
+ newlineEvent.text = QLatin1Char(kReturnCharCode);
+ newlineEvent.nativeVirtualKey = kVK_Return;
+ qCDebug(lcQpaKeys) << "Inserting newline via" << newlineEvent;
+ newlineEvent.sendWindowSystemEvent(m_platformWindow->window());
+
+ m_sendKeyEvent = false;
}
// ------------- Text composition -------------
diff --git a/src/plugins/platforms/cocoa/qnsview_keys.mm b/src/plugins/platforms/cocoa/qnsview_keys.mm
index 9628b770ad..fb55612212 100644
--- a/src/plugins/platforms/cocoa/qnsview_keys.mm
+++ b/src/plugins/platforms/cocoa/qnsview_keys.mm
@@ -55,7 +55,6 @@
window = popup->window();
}
- QBoolBlocker resendKeyEventGuard(m_resendKeyEvent, false);
// We will send a key event unless the input method sets m_sendKeyEvent to false
QBoolBlocker sendKeyEventGuard(m_sendKeyEvent, true);
@@ -108,9 +107,6 @@
}
}
-
- if (m_resendKeyEvent)
- m_sendKeyEvent = true;
}
bool accepted = true;