diff options
author | Tor Arne Vestbø <tor.arne.vestbo@qt.io> | 2020-09-14 18:44:09 +0200 |
---|---|---|
committer | Tor Arne Vestbø <tor.arne.vestbo@qt.io> | 2020-10-06 11:20:49 +0200 |
commit | 57d6f842eadcfe59337e223237ef024de3b0e433 (patch) | |
tree | 8dbdd5aea838064dda67974e5ab5713e37857951 | |
parent | 7aac974c78d488c1af39bb40feb5779c878dc4bf (diff) |
Un-special-case macOS in handling of QKeyEvent::nativeScanCode()
In the porting from Qt 4 to Qt 5 an assumption was made in QKeyMapper
that the underlying platform implementation needed the native scan
code to be able to resolve the possible keymaps for an event.
As a result, the macOS platform plugin started sending key events
with a fake native scan code of 1, so that it would still be allowed
to map key events.
Which in turn led to the documentation of QKeyEvent::nativeScanCode()
getting an exception for macOS.
Let's clean up this by removing the original assumption, and leave it
up to the platforms to decide what information from the key event
is needed.
QKeyMapperPrivate::possibleKeys() will still call extractKeyFromEvent
as a fallback if the platform layer doesn't return any possible keys.
Change-Id: I122a45bcec658c45ccc0b2c0671eb264d85d7be6
Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
-rw-r--r-- | src/gui/kernel/qevent.cpp | 4 | ||||
-rw-r--r-- | src/gui/kernel/qkeymapper.cpp | 3 | ||||
-rw-r--r-- | src/gui/platform/unix/qxkbcommon.cpp | 3 | ||||
-rw-r--r-- | src/plugins/platforms/cocoa/qcocoakeymapper.mm | 6 | ||||
-rw-r--r-- | src/plugins/platforms/cocoa/qnsview_keys.mm | 22 |
5 files changed, 20 insertions, 18 deletions
diff --git a/src/gui/kernel/qevent.cpp b/src/gui/kernel/qevent.cpp index 087873d67e..38906df6cb 100644 --- a/src/gui/kernel/qevent.cpp +++ b/src/gui/kernel/qevent.cpp @@ -1663,10 +1663,6 @@ QKeyEvent::~QKeyEvent() Note: The native scan code may be 0, even if the key event contains extended information. - - Note: On \macos, this function is not useful, because there is no - way to get the scan code from the system APIs. The function always - returns 1 (or 0 in the case explained above). */ /*! diff --git a/src/gui/kernel/qkeymapper.cpp b/src/gui/kernel/qkeymapper.cpp index 89eacc944a..19b0e15279 100644 --- a/src/gui/kernel/qkeymapper.cpp +++ b/src/gui/kernel/qkeymapper.cpp @@ -83,9 +83,6 @@ static QList<int> extractKeyFromEvent(QKeyEvent *e) QList<int> QKeyMapper::possibleKeys(QKeyEvent *e) { - if (!e->nativeScanCode()) - return extractKeyFromEvent(e); - return instance()->d_func()->possibleKeys(e); } diff --git a/src/gui/platform/unix/qxkbcommon.cpp b/src/gui/platform/unix/qxkbcommon.cpp index 71f1cfd24a..02b5ab5164 100644 --- a/src/gui/platform/unix/qxkbcommon.cpp +++ b/src/gui/platform/unix/qxkbcommon.cpp @@ -621,6 +621,9 @@ QList<int> QXkbCommon::possibleKeys(xkb_state *state, const QKeyEvent *event, { QList<int> result; quint32 keycode = event->nativeScanCode(); + if (!keycode) + return result; + Qt::KeyboardModifiers modifiers = event->modifiers(); xkb_keymap *keymap = xkb_state_get_keymap(state); // turn off the modifier bits which doesn't participate in shortcuts diff --git a/src/plugins/platforms/cocoa/qcocoakeymapper.mm b/src/plugins/platforms/cocoa/qcocoakeymapper.mm index 3b699e3237..dca7b576f6 100644 --- a/src/plugins/platforms/cocoa/qcocoakeymapper.mm +++ b/src/plugins/platforms/cocoa/qcocoakeymapper.mm @@ -513,7 +513,11 @@ QList<int> QCocoaKeyMapper::possibleKeys(const QKeyEvent *event) const { QList<int> ret; - auto keyMap = keyMapForKey(event->nativeVirtualKey(), QChar(event->key())); + const auto nativeVirtualKey = event->nativeVirtualKey(); + if (!nativeVirtualKey) + return ret; + + auto keyMap = keyMapForKey(nativeVirtualKey, QChar(event->key())); auto unmodifiedKey = keyMap[Qt::NoModifier]; Q_ASSERT(unmodifiedKey != Qt::Key_unknown); diff --git a/src/plugins/platforms/cocoa/qnsview_keys.mm b/src/plugins/platforms/cocoa/qnsview_keys.mm index f6599edb81..09d78485f4 100644 --- a/src/plugins/platforms/cocoa/qnsview_keys.mm +++ b/src/plugins/platforms/cocoa/qnsview_keys.mm @@ -53,11 +53,12 @@ m_inputSource = [characters retain]; } - // There is no way to get the scan code from carbon/cocoa. But we cannot - // use the value 0, since it indicates that the event originates from somewhere - // else than the keyboard. - quint32 nativeScanCode = 1; - quint32 nativeVirtualKey = [nsevent keyCode]; + // Scan codes are hardware dependent codes for each key. There is no way to get these + // from Carbon or Cocoa, so leave it 0, as documented in QKeyEvent::nativeScanCode(). + const quint32 nativeScanCode = 0; + + // Virtual keys on the other hand are mapped to be the same keys on any system + const quint32 nativeVirtualKey = nsevent.keyCode; QChar ch = QChar::ReplacementCharacter; int keyCode = Qt::Key_unknown; @@ -195,11 +196,12 @@ ulong nativeModifiers = [nsevent modifierFlags]; Qt::KeyboardModifiers modifiers = QCocoaKeyMapper::fromCocoaModifiers(nativeModifiers); - // There is no way to get the scan code from carbon/cocoa. But we cannot - // use the value 0, since it indicates that the event originates from somewhere - // else than the keyboard. - quint32 nativeScanCode = 1; - quint32 nativeVirtualKey = [nsevent keyCode]; + // Scan codes are hardware dependent codes for each key. There is no way to get these + // from Carbon or Cocoa, so leave it 0, as documented in QKeyEvent::nativeScanCode(). + const quint32 nativeScanCode = 0; + + // Virtual keys on the other hand are mapped to be the same keys on any system + const quint32 nativeVirtualKey = nsevent.keyCode; // calculate the delta and remember the current modifiers for next time static ulong m_lastKnownModifiers; |