[macOS] Add native virtual key code to modifier keys

Not forwarding the native virtual key code when sending key events
in the flagsChanged handler caused a number of problems, e.g. the
inability to determine which of the Alt or Shift keys were pressed.

Use handleExtendedKeyEvent to include the native virtual key code.

Patch inspired by Nyan Pasu.

Fixes: QTBUG-69608
Change-Id: I15e9ff1069528d4b50ee4638ab2d8a6fd279db0b
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
This commit is contained in:
Michael Brüning 2020-08-24 18:07:23 +02:00
parent a4e4436e85
commit ac310d5b23

View File

@ -192,14 +192,20 @@
- (void)flagsChanged:(NSEvent *)nsevent
{
ulong timestamp = [nsevent timestamp] * 1000;
ulong modifiers = [nsevent modifierFlags];
Qt::KeyboardModifiers qmodifiers = QCocoaKeyMapper::fromCocoaModifiers(modifiers);
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];
// calculate the delta and remember the current modifiers for next time
static ulong m_lastKnownModifiers;
ulong lastKnownModifiers = m_lastKnownModifiers;
ulong delta = lastKnownModifiers ^ modifiers;
m_lastKnownModifiers = modifiers;
ulong delta = lastKnownModifiers ^ nativeModifiers;
m_lastKnownModifiers = nativeModifiers;
struct qt_mac_enum_mapper
{
@ -225,11 +231,14 @@
else if (qtCode == Qt::Key_Control)
qtCode = Qt::Key_Meta;
}
QWindowSystemInterface::handleKeyEvent(m_platformWindow->window(),
timestamp,
(lastKnownModifiers & mac_mask) ? QEvent::KeyRelease : QEvent::KeyPress,
qtCode,
qmodifiers ^ QCocoaKeyMapper::fromCocoaModifiers(mac_mask));
QWindowSystemInterface::handleExtendedKeyEvent(m_platformWindow->window(),
timestamp,
(lastKnownModifiers & mac_mask) ? QEvent::KeyRelease
: QEvent::KeyPress,
qtCode,
modifiers ^ QCocoaKeyMapper::fromCocoaModifiers(mac_mask),
nativeScanCode, nativeVirtualKey,
nativeModifiers ^ mac_mask);
}
}