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
Pick-to: 6.2 6.3 6.4
Change-Id: I86d30b154867e8e2b6964967ede2bd0dadb83ef8
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
Tor Arne Vestbø 2022-07-06 14:40:01 +02:00
parent 3ad0f755f5
commit 6767ac20c9

View File

@ -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());
}