macOS: support shortcut for Ctrl-modified Tab

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.

Fixes: QTBUG-113492
Task-number: QTBUG-8596
Change-Id: Id3aa7021a689b94d97eb881b19ddf7cb039e1bd2
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
(cherry picked from commit 285294b684ac973efcf2c8ff92b427db3fb75948)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Volker Hilsheimer 2024-04-11 15:20:52 +02:00 committed by Qt Cherry-pick Bot
parent 418827f7cb
commit a49960be79

View File

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