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>
This commit is contained in:
Tor Arne Vestbø 2020-09-14 18:44:09 +02:00
parent 7aac974c78
commit 57d6f842ea
5 changed files with 20 additions and 18 deletions

View File

@ -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).
*/
/*!

View File

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

View File

@ -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

View File

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

View File

@ -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;