From 285294b684ac973efcf2c8ff92b427db3fb75948 Mon Sep 17 00:00:00 2001 From: Volker Hilsheimer Date: Thu, 11 Apr 2024 15:20:52 +0200 Subject: macOS: support shortcut for Ctrl-modified Tab MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Ctrl+(Shift/Option/Cmd)+Tab presses are not handled as a regular key events, and are not seen by our NSView.keyDown implementation. But they are delivered to a performKeyEquivalent implementation, so if we see a Tab key being pressed with the Ctrl modifier down, then send a shortcut event and stop processing if that event got accepted by Qt. Pick-to: 6.7 Fixes: QTBUG-113492 Task-number: QTBUG-8596 Change-Id: Id3aa7021a689b94d97eb881b19ddf7cb039e1bd2 Reviewed-by: Tor Arne Vestbø --- src/plugins/platforms/cocoa/qnsview_keys.mm | 39 +++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 10 deletions(-) (limited to 'src/plugins/platforms') diff --git a/src/plugins/platforms/cocoa/qnsview_keys.mm b/src/plugins/platforms/cocoa/qnsview_keys.mm index d9d9f0a794..abee622e65 100644 --- a/src/plugins/platforms/cocoa/qnsview_keys.mm +++ b/src/plugins/platforms/cocoa/qnsview_keys.mm @@ -30,8 +30,36 @@ static bool isSpecialKey(const QString &text) return false; } +static bool sendAsShortcut(const KeyEvent &keyEvent, QWindow *window) +{ + KeyEvent shortcutEvent = keyEvent; + shortcutEvent.type = QEvent::Shortcut; + qCDebug(lcQpaKeys) << "Trying potential shortcuts in" << window + << "for" << shortcutEvent; + + if (shortcutEvent.sendWindowSystemEvent(window)) { + qCDebug(lcQpaKeys) << "Found matching shortcut; will not send as key event"; + return true; + } + qCDebug(lcQpaKeys) << "No matching shortcuts; continuing with key event delivery"; + return false; +} + @implementation QNSView (Keys) +- (bool)performKeyEquivalent:(NSEvent *)nsevent +{ + // Implemented to handle shortcuts for modified Tab keys, which are + // handled by Cocoa and not delivered to your keyDown implementation. + if (nsevent.type == NSEventTypeKeyDown && m_composingText.isEmpty()) { + const bool ctrlDown = [nsevent modifierFlags] & NSEventModifierFlagControl; + const bool isTabKey = nsevent.keyCode == kVK_Tab; + if (ctrlDown && isTabKey && sendAsShortcut(KeyEvent(nsevent), [self topLevelWindow])) + return YES; + } + return NO; +} + - (bool)handleKeyEvent:(NSEvent *)nsevent { qCDebug(lcQpaKeys) << "Handling" << nsevent; @@ -52,17 +80,8 @@ static bool isSpecialKey(const QString &text) if (keyEvent.type == QEvent::KeyPress) { if (m_composingText.isEmpty()) { - KeyEvent shortcutEvent = keyEvent; - shortcutEvent.type = QEvent::Shortcut; - qCDebug(lcQpaKeys) << "Trying potential shortcuts in" << window - << "for" << shortcutEvent; - - if (shortcutEvent.sendWindowSystemEvent(window)) { - qCDebug(lcQpaKeys) << "Found matching shortcut; will not send as key event"; + if (sendAsShortcut(keyEvent, window)) return true; - } else { - qCDebug(lcQpaKeys) << "No matching shortcuts; continuing with key event delivery"; - } } QObject *focusObject = m_platformWindow ? m_platformWindow->window()->focusObject() : nullptr; -- cgit v1.2.3