From f3e861819d0303f7d69d17990f45177df65eaec7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Wed, 27 Jan 2021 18:32:48 +0100 Subject: macOS: Don't wrap key event keys in QChar MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Key events have a wider range of possible values than the unicode range, as they also include all the special keys as defined in Qt::Keys. Instead of turning the argument to keyMapForKey into an integer, we can remove the argument completely, as it was only used as a fallback in the cases where UCKeyTranslate (or in the future, charactersByApplyingModifiers:), failed to map the virtual key and modifiers to a character. But in those cases we should not fall back to the Qt key from the key event, as that doesn't match what the keyboard layout defines. Most keyboard layouts explicitly define the base key as the key for these "undefined" mappings, but if the keyboard layout does not, we should not produce any input in the application, to match what AppKit does in this case. Fixes: QTBUG-90315 Fixes: QTBUG-92891 Change-Id: Ib9ffd9521049ee8e4b103597c1d34cbe3d23dbdf Reviewed-by: Timur Pocheptsov Reviewed-by: Morten Johan Sørvig (cherry picked from commit 6faa33192c99f3432b28591b991918b47bd6fa09) Reviewed-by: Tor Arne Vestbø --- src/gui/platform/darwin/qapplekeymapper.mm | 28 ++++++++++++++++------------ src/gui/platform/darwin/qapplekeymapper_p.h | 2 +- 2 files changed, 17 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/gui/platform/darwin/qapplekeymapper.mm b/src/gui/platform/darwin/qapplekeymapper.mm index 760957ba2a..a1cd0a6abd 100644 --- a/src/gui/platform/darwin/qapplekeymapper.mm +++ b/src/gui/platform/darwin/qapplekeymapper.mm @@ -481,7 +481,7 @@ static constexpr Qt::KeyboardModifiers modifierCombinations[] = { Returns a key map for the given \virtualKey based on all possible modifier combinations. */ -const QAppleKeyMapper::KeyMap &QAppleKeyMapper::keyMapForKey(VirtualKeyCode virtualKey, QChar unicodeKey) const +const QAppleKeyMapper::KeyMap &QAppleKeyMapper::keyMapForKey(VirtualKeyCode virtualKey) const { static_assert(sizeof(modifierCombinations) / sizeof(Qt::KeyboardModifiers) == kNumModifierCombinations); @@ -491,7 +491,7 @@ const QAppleKeyMapper::KeyMap &QAppleKeyMapper::keyMapForKey(VirtualKeyCode virt if (keyMap[Qt::NoModifier] != Qt::Key_unknown) return keyMap; // Already filled - qCDebug(lcQpaKeyMapper, "Updating key map for virtual key = 0x%02x!", (uint)virtualKey); + qCDebug(lcQpaKeyMapper, "Updating key map for virtual key 0x%02x", (uint)virtualKey); // Key mapping via [NSEvent charactersByApplyingModifiers:] only works for key down // events, but we might (wrongly) get into this code path for other key events such @@ -515,9 +515,10 @@ const QAppleKeyMapper::KeyMap &QAppleKeyMapper::keyMapForKey(VirtualKeyCode virt kUCKeyActionDown, modifierKeyState, m_keyboardKind, OptionBits(0), &m_deadKeyState, maxStringLength, &actualStringLength, unicodeString); - // Use translated unicode key if valid + // Use translated Unicode key if valid + QChar carbonUnicodeKey; if (err == noErr && actualStringLength) - unicodeKey = QChar(unicodeString[0]); + carbonUnicodeKey = QChar(unicodeString[0]); if (@available(macOS 10.15, *)) { if (canMapCocoaEvent) { @@ -528,21 +529,24 @@ const QAppleKeyMapper::KeyMap &QAppleKeyMapper::keyMapForKey(VirtualKeyCode virt auto *charactersWithModifiers = [NSApp.currentEvent charactersByApplyingModifiers:cocoaModifiers]; Q_ASSERT(charactersWithModifiers && charactersWithModifiers.length > 0); auto cocoaUnicodeKey = QChar([charactersWithModifiers characterAtIndex:0]); - if (cocoaUnicodeKey != unicodeKey) { + if (cocoaUnicodeKey != carbonUnicodeKey) { qCWarning(lcQpaKeyMapper) << "Mismatch between Cocoa" << cocoaUnicodeKey - << "and Carbon" << unicodeKey << "for virtual key" << virtualKey + << "and Carbon" << carbonUnicodeKey << "for virtual key" << virtualKey << "with" << qtModifiers; } } } - int qtkey = toKeyCode(unicodeKey, virtualKey, qtModifiers); - if (qtkey == Qt::Key_unknown) - qtkey = unicodeKey.unicode(); + int qtKey = toKeyCode(carbonUnicodeKey, virtualKey, qtModifiers); + if (qtKey == Qt::Key_unknown) + qtKey = carbonUnicodeKey.unicode(); - keyMap[i] = qtkey; + keyMap[i] = qtKey; - qCDebug(lcQpaKeyMapper, " [%d] (%d,0x%02x,'%c')", i, qtkey, qtkey, qtkey); + qCDebug(lcQpaKeyMapper).verbosity(0) << "\t" << qtModifiers + << "+" << qUtf8Printable(QString::asprintf("0x%02x", virtualKey)) + << "=" << qUtf8Printable(QString::asprintf("%d / 0x%02x /", qtKey, qtKey)) + << QString::asprintf("%c", qtKey); } return keyMap; @@ -556,7 +560,7 @@ QList QAppleKeyMapper::possibleKeys(const QKeyEvent *event) const if (!nativeVirtualKey) return ret; - auto keyMap = keyMapForKey(nativeVirtualKey, QChar(event->key())); + auto keyMap = keyMapForKey(nativeVirtualKey); auto unmodifiedKey = keyMap[Qt::NoModifier]; Q_ASSERT(unmodifiedKey != Qt::Key_unknown); diff --git a/src/gui/platform/darwin/qapplekeymapper_p.h b/src/gui/platform/darwin/qapplekeymapper_p.h index aed9907d92..78e71ac37d 100644 --- a/src/gui/platform/darwin/qapplekeymapper_p.h +++ b/src/gui/platform/darwin/qapplekeymapper_p.h @@ -95,7 +95,7 @@ private: bool updateKeyboard(); using VirtualKeyCode = unsigned short; - const KeyMap &keyMapForKey(VirtualKeyCode virtualKey, QChar unicodeKey) const; + const KeyMap &keyMapForKey(VirtualKeyCode virtualKey) const; QCFType m_currentInputSource = nullptr; -- cgit v1.2.3