macOS: Add QCocoaKeyMapper::toCocoaModifiers

Change-Id: I489c64733275260bb041f8df5cc5ff4a571d4e9c
Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
This commit is contained in:
Tor Arne Vestbø 2020-07-09 17:05:55 +02:00
parent 0ef46b41d8
commit 7917f9b7f5
4 changed files with 18 additions and 23 deletions

View File

@ -714,7 +714,7 @@ qt_feature("openvg" PUBLIC
LABEL "OpenVG"
CONDITION libs.openvg OR FIXME
)
qt_feature("egl" PUBLIC PRIVATE
qt_feature("egl" PUBLIC
LABEL "EGL"
CONDITION ( QT_FEATURE_opengl OR QT_FEATURE_openvg ) AND EGL_FOUND AND ( QT_FEATURE_dlopen OR NOT UNIX OR INTEGRITY )
)

View File

@ -83,6 +83,8 @@ public:
QList<int> possibleKeys(const QKeyEvent *event) const;
static Qt::KeyboardModifiers fromCocoaModifiers(NSEventModifierFlags cocoaModifiers);
static NSEventModifierFlags toCocoaModifiers(Qt::KeyboardModifiers);
static QChar toCocoaKey(Qt::Key key);
static Qt::Key fromCocoaKey(QChar keyCode);

View File

@ -84,6 +84,19 @@ Qt::KeyboardModifiers QCocoaKeyMapper::fromCocoaModifiers(NSEventModifierFlags c
return swapModifiersIfNeeded(qtModifiers);
}
NSEventModifierFlags QCocoaKeyMapper::toCocoaModifiers(Qt::KeyboardModifiers qtModifiers)
{
qtModifiers = swapModifiersIfNeeded(qtModifiers);
NSEventModifierFlags cocoaModifiers = 0;
for (const auto &[cocoaModifier, qtModifier] : cocoaModifierMap) {
if (qtModifiers & qtModifier)
cocoaModifiers |= cocoaModifier;
}
return cocoaModifiers;
}
using CarbonModifiers = UInt32; // As opposed to EventModifiers which is UInt16
static CarbonModifiers toCarbonModifiers(Qt::KeyboardModifiers qtModifiers)

View File

@ -80,21 +80,6 @@ QString qt_mac_applicationmenu_string(int type)
}
}
static quint32 constructModifierMask(quint32 accel_key)
{
quint32 ret = 0;
const bool dontSwap = qApp->testAttribute(Qt::AA_MacDontSwapCtrlAndMeta);
if ((accel_key & Qt::CTRL) == Qt::CTRL)
ret |= (dontSwap ? NSEventModifierFlagControl : NSEventModifierFlagCommand);
if ((accel_key & Qt::META) == Qt::META)
ret |= (dontSwap ? NSEventModifierFlagCommand : NSEventModifierFlagControl);
if ((accel_key & Qt::ALT) == Qt::ALT)
ret |= NSEventModifierFlagOption;
if ((accel_key & Qt::SHIFT) == Qt::SHIFT)
ret |= NSEventModifierFlagShift;
return ret;
}
#ifndef QT_NO_SHORTCUT
// return an autoreleased string given a QKeySequence (currently only looks at the first one).
NSString *keySequenceToKeyEqivalent(const QKeySequence &accel)
@ -108,12 +93,6 @@ NSString *keySequenceToKeyEqivalent(const QKeySequence &accel)
cocoa_key = NSDeleteCharacter;
return QStringView{&cocoa_key, 1}.toNSString();
}
// return the cocoa modifier mask for the QKeySequence (currently only looks at the first one).
NSUInteger keySequenceModifierMask(const QKeySequence &accel)
{
return constructModifierMask(accel[0]);
}
#endif
QCocoaMenuItem::QCocoaMenuItem() :
@ -390,7 +369,8 @@ NSMenuItem *QCocoaMenuItem::sync()
#ifndef QT_NO_SHORTCUT
if (accel.count() == 1) {
m_native.keyEquivalent = keySequenceToKeyEqivalent(accel);
m_native.keyEquivalentModifierMask = keySequenceModifierMask(accel);
auto modifiers = Qt::KeyboardModifiers(accel[0] & Qt::KeyboardModifierMask);
m_native.keyEquivalentModifierMask = QCocoaKeyMapper::toCocoaModifiers(modifiers);
} else
#endif
{