From 13118701255b5e6835c018e9a9f8741140f768bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Wed, 6 Jul 2022 14:40:01 +0200 Subject: [PATCH] macOS: Distinguish between return and enter when inserting newlines In most situations the originating key event contains the information we need, but during confirmation of input method composition the text of the event might be "~\r", in which case the logic we have on macOS for generating key events will interpret the key press as a tilde, and we end up inserting "~~" and fail to deliver the return key. We should probably treat NSEvents with characters > 1 as a special case and go via the virtual key map in QAppleKeyMapper, but for now we manually distinguish enter and return by looking at the modifiers. This works for enter key presses both via the key itself, and via Fn+Return. Fixes: QTBUG-104774 Fixes: QTBUG-103473 Change-Id: I86d30b154867e8e2b6964967ede2bd0dadb83ef8 Reviewed-by: Volker Hilsheimer (cherry picked from commit 6767ac20c972f0f2654076c22826c392e698edfc) Reviewed-by: Qt Cherry-pick Bot --- src/plugins/platforms/cocoa/qnsview_complextext.mm | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/plugins/platforms/cocoa/qnsview_complextext.mm b/src/plugins/platforms/cocoa/qnsview_complextext.mm index 4be81262994..ad5f5a0827f 100644 --- a/src/plugins/platforms/cocoa/qnsview_complextext.mm +++ b/src/plugins/platforms/cocoa/qnsview_complextext.mm @@ -112,9 +112,14 @@ KeyEvent newlineEvent(m_currentlyInterpretedKeyEvent ? m_currentlyInterpretedKeyEvent : NSApp.currentEvent); newlineEvent.type = QEvent::KeyPress; - newlineEvent.key = Qt::Key_Return; - newlineEvent.text = QLatin1Char(kReturnCharCode); - newlineEvent.nativeVirtualKey = kVK_Return; + + const bool isEnter = newlineEvent.modifiers & Qt::KeypadModifier; + newlineEvent.key = isEnter ? Qt::Key_Enter : Qt::Key_Return; + newlineEvent.text = isEnter ? QLatin1Char(kEnterCharCode) + : QLatin1Char(kReturnCharCode); + newlineEvent.nativeVirtualKey = isEnter ? kVK_ANSI_KeypadEnter + : kVK_Return; + qCDebug(lcQpaKeys) << "Inserting newline via" << newlineEvent; newlineEvent.sendWindowSystemEvent(m_platformWindow->window()); }