diff options
author | Tor Arne Vestbø <tor.arne.vestbo@qt.io> | 2021-12-21 19:23:57 +0100 |
---|---|---|
committer | Tor Arne Vestbø <tor.arne.vestbo@qt.io> | 2021-12-22 01:34:56 +0100 |
commit | 7bd243b3fb7ee068ccf69ef7181acfac56e4e7ed (patch) | |
tree | cc998e521964dc10ded997204a1a88e7ad92a20a /src/plugins | |
parent | 5d04c27a272ab1c0ccc4bfd40f075e1cb7cfd8a2 (diff) |
macOS: Don't read keyCode of non-key events when inserting newlines
When the system input method decides to trigger an insertNewline
message it might come from a synthetic NSEvent of type AppKitDefined,
which does not respond to the keyCode message. Accessing the keyCode
would result in an exception being thrown.
We guard for this scenario by moving the keyCode access into the
key-press and -release specific parts of the KeyEvent constructor,
making the KeyEvent usable with generic NSEvents (even if that
means it will only pull out the timestamp and modifiers).
We also prefer to use the currently interpreted NSEvent, if one is
available, when preparing the synthetic newline event. This ensures
that we get a valid timestamp and modifiers, since the synthetic
AppKitDefined event that the system IM generates does not provide
valid values for these.
Fixes: QTBUG-99186
Pick-to: 6.2 6.3
Change-Id: I050ae2aa2d8ded1be3541f746e6c052f3546e27c
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Diffstat (limited to 'src/plugins')
-rw-r--r-- | src/plugins/platforms/cocoa/qnsview_complextext.mm | 4 | ||||
-rw-r--r-- | src/plugins/platforms/cocoa/qnsview_keys.mm | 3 |
2 files changed, 5 insertions, 2 deletions
diff --git a/src/plugins/platforms/cocoa/qnsview_complextext.mm b/src/plugins/platforms/cocoa/qnsview_complextext.mm index 39739d9725..c1e1364f32 100644 --- a/src/plugins/platforms/cocoa/qnsview_complextext.mm +++ b/src/plugins/platforms/cocoa/qnsview_complextext.mm @@ -145,7 +145,9 @@ // e.g. "~\r". We have already inserted the composition, // so we need to follow up with a single newline event. - KeyEvent newlineEvent(NSApp.currentEvent); + KeyEvent newlineEvent(m_currentlyInterpretedKeyEvent ? + m_currentlyInterpretedKeyEvent : NSApp.currentEvent); + newlineEvent.type = QEvent::KeyPress; newlineEvent.key = Qt::Key_Return; newlineEvent.text = QLatin1Char(kReturnCharCode); newlineEvent.nativeVirtualKey = kVK_Return; diff --git a/src/plugins/platforms/cocoa/qnsview_keys.mm b/src/plugins/platforms/cocoa/qnsview_keys.mm index 9acd5cd14a..01c3a4ea0d 100644 --- a/src/plugins/platforms/cocoa/qnsview_keys.mm +++ b/src/plugins/platforms/cocoa/qnsview_keys.mm @@ -235,7 +235,6 @@ KeyEvent::KeyEvent(NSEvent *nsevent) { timestamp = nsevent.timestamp * 1000; nativeModifiers = nsevent.modifierFlags; - nativeVirtualKey = nsevent.keyCode; modifiers = QAppleKeyMapper::fromCocoaModifiers(nativeModifiers); switch (nsevent.type) { @@ -245,6 +244,8 @@ KeyEvent::KeyEvent(NSEvent *nsevent) } if (nsevent.type == NSEventTypeKeyDown || nsevent.type == NSEventTypeKeyUp) { + nativeVirtualKey = nsevent.keyCode; + NSString *charactersIgnoringModifiers = nsevent.charactersIgnoringModifiers; NSString *characters = nsevent.characters; |