diff options
author | Tor Arne Vestbø <tor.arne.vestbo@qt.io> | 2021-01-27 18:32:48 +0100 |
---|---|---|
committer | Tor Arne Vestbø <tor.arne.vestbo@qt.io> | 2021-02-05 15:52:28 +0100 |
commit | 38c6e5ebbd0d4d9f956420a02fc17b952cf5818b (patch) | |
tree | b761abd490dd66f40bc4b08d19ef32f4398f8cbe | |
parent | d482d8b1b5ecf447c41f47db140f1c9e15177477 (diff) |
macOS: Don't wrap key event keys in QChar
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
Change-Id: Ib9ffd9521049ee8e4b103597c1d34cbe3d23dbdf
Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
Reviewed-by: Morten Johan Sørvig <morten.sorvig@qt.io>
(cherry picked from commit 6faa33192c99f3432b28591b991918b47bd6fa09)
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
-rw-r--r-- | src/plugins/platforms/cocoa/qcocoakeymapper.h | 2 | ||||
-rw-r--r-- | src/plugins/platforms/cocoa/qcocoakeymapper.mm | 28 |
2 files changed, 17 insertions, 13 deletions
diff --git a/src/plugins/platforms/cocoa/qcocoakeymapper.h b/src/plugins/platforms/cocoa/qcocoakeymapper.h index dbf164c18e..e18c6e71fa 100644 --- a/src/plugins/platforms/cocoa/qcocoakeymapper.h +++ b/src/plugins/platforms/cocoa/qcocoakeymapper.h @@ -75,7 +75,7 @@ private: bool updateKeyboard(); using VirtualKeyCode = unsigned short; - const KeyMap &keyMapForKey(VirtualKeyCode virtualKey, QChar unicodeKey) const; + const KeyMap &keyMapForKey(VirtualKeyCode virtualKey) const; QCFType<TISInputSourceRef> m_currentInputSource = nullptr; diff --git a/src/plugins/platforms/cocoa/qcocoakeymapper.mm b/src/plugins/platforms/cocoa/qcocoakeymapper.mm index caa68ae694..6c378d73b4 100644 --- a/src/plugins/platforms/cocoa/qcocoakeymapper.mm +++ b/src/plugins/platforms/cocoa/qcocoakeymapper.mm @@ -442,7 +442,7 @@ static constexpr Qt::KeyboardModifiers modifierCombinations[] = { Returns a key map for the given \virtualKey based on all possible modifier combinations. */ -const QCocoaKeyMapper::KeyMap &QCocoaKeyMapper::keyMapForKey(VirtualKeyCode virtualKey, QChar unicodeKey) const +const QCocoaKeyMapper::KeyMap &QCocoaKeyMapper::keyMapForKey(VirtualKeyCode virtualKey) const { static_assert(sizeof(modifierCombinations) / sizeof(Qt::KeyboardModifiers) == kNumModifierCombinations); @@ -452,7 +452,7 @@ const QCocoaKeyMapper::KeyMap &QCocoaKeyMapper::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 @@ -476,9 +476,10 @@ const QCocoaKeyMapper::KeyMap &QCocoaKeyMapper::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) { @@ -489,21 +490,24 @@ const QCocoaKeyMapper::KeyMap &QCocoaKeyMapper::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; @@ -517,7 +521,7 @@ QList<int> QCocoaKeyMapper::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); |