8#include <AppKit/AppKit.h>
11#if defined(QT_PLATFORM_UIKIT)
12#include <UIKit/UIKit.h>
17#include <QtCore/qloggingcategory.h>
18#include <QtGui/QGuiApplication>
23Q_STATIC_LOGGING_CATEGORY(lcQpaKeyMapperKeys,
"qt.qpa.keymapper.keys");
28 if (QCoreApplication::testAttribute(Qt::AA_MacDontSwapCtrlAndMeta))
31 Qt::KeyboardModifiers swappedModifiers = modifiers;
32 swappedModifiers &= ~(Qt::MetaModifier | Qt::ControlModifier);
34 if (modifiers & Qt::ControlModifier)
35 swappedModifiers |= Qt::MetaModifier;
36 if (modifiers & Qt::MetaModifier)
37 swappedModifiers |= Qt::ControlModifier;
39 return swappedModifiers;
44 if (QCoreApplication::testAttribute(Qt::AA_MacDontSwapCtrlAndMeta))
47 if (key == Qt::Key_Meta)
48 return Qt::Key_Control;
49 else if (key == Qt::Key_Control)
56static constexpr std::tuple<NSEventModifierFlags, Qt::KeyboardModifier> cocoaModifierMap[] = {
57 { NSEventModifierFlagShift, Qt::ShiftModifier },
58 { NSEventModifierFlagControl, Qt::ControlModifier },
59 { NSEventModifierFlagCommand, Qt::MetaModifier },
60 { NSEventModifierFlagOption, Qt::AltModifier },
61 { NSEventModifierFlagNumericPad, Qt::KeypadModifier }
64Qt::KeyboardModifiers QAppleKeyMapper::fromCocoaModifiers(NSEventModifierFlags cocoaModifiers)
66 Qt::KeyboardModifiers qtModifiers = Qt::NoModifier;
67 for (
const auto &[cocoaModifier, qtModifier] : cocoaModifierMap) {
68 if (cocoaModifiers & cocoaModifier)
69 qtModifiers |= qtModifier;
72 return swapModifiersIfNeeded(qtModifiers);
75NSEventModifierFlags QAppleKeyMapper::toCocoaModifiers(Qt::KeyboardModifiers qtModifiers)
77 qtModifiers = swapModifiersIfNeeded(qtModifiers);
79 NSEventModifierFlags cocoaModifiers = 0;
80 for (
const auto &[cocoaModifier, qtModifier] : cocoaModifierMap) {
81 if (qtModifiers & qtModifier)
82 cocoaModifiers |= cocoaModifier;
85 return cocoaModifiers;
88using CarbonModifiers = UInt32;
90static CarbonModifiers toCarbonModifiers(Qt::KeyboardModifiers qtModifiers)
92 qtModifiers = swapModifiersIfNeeded(qtModifiers);
94 static constexpr std::tuple<
int, Qt::KeyboardModifier> carbonModifierMap[] = {
95 { shiftKey, Qt::ShiftModifier },
96 { controlKey, Qt::ControlModifier },
97 { cmdKey, Qt::MetaModifier },
98 { optionKey, Qt::AltModifier },
99 { kEventKeyModifierNumLockMask, Qt::KeypadModifier }
102 CarbonModifiers carbonModifiers = 0;
103 for (
const auto &[carbonModifier, qtModifier] : carbonModifierMap) {
104 if (qtModifiers & qtModifier)
105 carbonModifiers |= carbonModifier;
108 return carbonModifiers;
114static QHash<
char16_t, Qt::Key> layoutIndependentKeyCodes = {
115 { kVK_F1, Qt::Key_F1 },
116 { kVK_F2, Qt::Key_F2 },
117 { kVK_F3, Qt::Key_F3 },
118 { kVK_F4, Qt::Key_F4 },
119 { kVK_F5, Qt::Key_F5 },
120 { kVK_F6, Qt::Key_F6 },
121 { kVK_F7, Qt::Key_F7 },
122 { kVK_F8, Qt::Key_F8 },
123 { kVK_F9, Qt::Key_F9 },
124 { kVK_F10, Qt::Key_F10 },
125 { kVK_F11, Qt::Key_F11 },
126 { kVK_F12, Qt::Key_F12 },
127 { kVK_F13, Qt::Key_F13 },
128 { kVK_F14, Qt::Key_F14 },
129 { kVK_F15, Qt::Key_F15 },
130 { kVK_F16, Qt::Key_F16 },
131 { kVK_F17, Qt::Key_F17 },
132 { kVK_F18, Qt::Key_F18 },
133 { kVK_F19, Qt::Key_F19 },
134 { kVK_F20, Qt::Key_F20 },
136 { kVK_Return, Qt::Key_Return },
137 { kVK_Tab, Qt::Key_Tab },
138 { kVK_Space, Qt::Key_Space },
139 { kVK_Escape, Qt::Key_Escape },
140 { kVK_Delete, Qt::Key_Backspace },
141 { kVK_ForwardDelete, Qt::Key_Delete },
143 { kVK_Home, Qt::Key_Home },
144 { kVK_End, Qt::Key_End },
145 { kVK_PageUp, Qt::Key_PageUp },
146 { kVK_PageDown, Qt::Key_PageDown },
148 { kVK_UpArrow, Qt::Key_Up },
149 { kVK_DownArrow, Qt::Key_Down },
150 { kVK_LeftArrow, Qt::Key_Left },
151 { kVK_RightArrow, Qt::Key_Right },
153 { kVK_CapsLock, Qt::Key_CapsLock },
154 { kVK_Shift, Qt::Key_Shift },
155 { kVK_RightShift, Qt::Key_Shift },
161 { kVK_Command, Qt::Key_unknown },
162 { kVK_RightCommand, Qt::Key_unknown },
163 { kVK_Option, Qt::Key_unknown },
164 { kVK_RightOption, Qt::Key_unknown },
165 { kVK_Control, Qt::Key_unknown },
166 { kVK_RightControl, Qt::Key_unknown },
167 { kVK_Function, Qt::Key_unknown },
170 { kVK_VolumeUp, Qt::Key_VolumeUp },
171 { kVK_VolumeDown, Qt::Key_VolumeDown },
172 { kVK_Mute, Qt::Key_VolumeMute },
176 { kVK_ContextualMenu, Qt::Key_unknown },
178 { kVK_Help, Qt::Key_Help },
180 { kVK_ANSI_KeypadClear, Qt::Key_Clear },
181 { kVK_ANSI_KeypadEnter, Qt::Key_Enter },
184static QHash<
char16_t, Qt::Key> functionKeys = {
185 { NSUpArrowFunctionKey, Qt::Key_Up },
186 { NSDownArrowFunctionKey, Qt::Key_Down },
187 { NSLeftArrowFunctionKey, Qt::Key_Left },
188 { NSRightArrowFunctionKey, Qt::Key_Right },
190 { NSInsertFunctionKey, Qt::Key_Insert },
191 { NSDeleteFunctionKey, Qt::Key_Delete },
192 { NSHomeFunctionKey, Qt::Key_Home },
193 { NSEndFunctionKey, Qt::Key_End },
194 { NSPageUpFunctionKey, Qt::Key_PageUp },
195 { NSPageDownFunctionKey, Qt::Key_PageDown },
196 { NSPrintScreenFunctionKey, Qt::Key_Print },
197 { NSScrollLockFunctionKey, Qt::Key_ScrollLock },
198 { NSPauseFunctionKey, Qt::Key_Pause },
199 { NSSysReqFunctionKey, Qt::Key_SysReq },
200 { NSMenuFunctionKey, Qt::Key_Menu },
201 { NSPrintFunctionKey, Qt::Key_Printer },
202 { NSClearDisplayFunctionKey, Qt::Key_Clear },
203 { NSInsertCharFunctionKey, Qt::Key_Insert },
204 { NSDeleteCharFunctionKey, Qt::Key_Delete },
205 { NSSelectFunctionKey, Qt::Key_Select },
206 { NSExecuteFunctionKey, Qt::Key_Execute },
207 { NSUndoFunctionKey, Qt::Key_Undo },
208 { NSRedoFunctionKey, Qt::Key_Redo },
209 { NSFindFunctionKey, Qt::Key_Find },
210 { NSHelpFunctionKey, Qt::Key_Help },
211 { NSModeSwitchFunctionKey, Qt::Key_Mode_switch }
214static int toKeyCode(
const QChar &key,
int virtualKey,
int modifiers)
216 qCDebug(lcQpaKeyMapperKeys,
"Mapping key: %d (0x%04x) / vk %d (0x%04x)",
217 key.unicode(), key.unicode(), virtualKey, virtualKey);
221 if (
auto qtKey = layoutIndependentKeyCodes.value(virtualKey)) {
222 qCDebug(lcQpaKeyMapperKeys) <<
"Got" << qtKey <<
"based on layout independent virtual key";
224 if (qtKey == Qt::Key_Tab && (modifiers & Qt::ShiftModifier)) {
225 qCDebug(lcQpaKeyMapperKeys,
"Transformed into Qt::Key_Backtab");
226 return Qt::Key_Backtab;
232 if (key >=
char16_t(NSUpArrowFunctionKey) && key <=
char16_t(NSModeSwitchFunctionKey)) {
233 if (
auto qtKey = functionKeys.value(key.unicode())) {
234 qCDebug(lcQpaKeyMapperKeys) <<
"Got" << qtKey;
236 }
else if (key >=
char16_t(NSF1FunctionKey) && key <=
char16_t(NSF35FunctionKey)) {
237 auto functionKey = Qt::Key_F1 + (key.unicode() - NSF1FunctionKey) ;
238 qCDebug(lcQpaKeyMapperKeys) <<
"Got" << functionKey;
244 qCDebug(lcQpaKeyMapperKeys,
"Got digit key: %d", key.digitValue());
245 return key.digitValue() + Qt::Key_0;
248 if (key.isLetter()) {
249 qCDebug(lcQpaKeyMapperKeys,
"Got letter key: %d", (key.toUpper().unicode() -
'A'));
250 return (key.toUpper().unicode() -
'A') + Qt::Key_A;
252 if (key.isSymbol()) {
253 qCDebug(lcQpaKeyMapperKeys,
"Got symbol key: %d", (key.unicode()));
254 return key.unicode();
257 qCDebug(lcQpaKeyMapperKeys,
"Unknown case.. %d[%d] %d", key.unicode(), key.toLatin1(), virtualKey);
258 return Qt::Key_unknown;
263static const int NSEscapeCharacter = 27;
265static const QHash<
char16_t, Qt::Key> cocoaKeys = {
266 { NSEnterCharacter, Qt::Key_Enter },
267 { NSBackspaceCharacter, Qt::Key_Backspace },
268 { NSTabCharacter, Qt::Key_Tab },
269 { NSNewlineCharacter, Qt::Key_Return },
270 { NSCarriageReturnCharacter, Qt::Key_Return },
271 { NSBackTabCharacter, Qt::Key_Backtab },
272 { NSEscapeCharacter, Qt::Key_Escape },
273 { NSDeleteCharacter, Qt::Key_Backspace },
274 { NSUpArrowFunctionKey, Qt::Key_Up },
275 { NSDownArrowFunctionKey, Qt::Key_Down },
276 { NSLeftArrowFunctionKey, Qt::Key_Left },
277 { NSRightArrowFunctionKey, Qt::Key_Right },
278 { NSF1FunctionKey, Qt::Key_F1 },
279 { NSF2FunctionKey, Qt::Key_F2 },
280 { NSF3FunctionKey, Qt::Key_F3 },
281 { NSF4FunctionKey, Qt::Key_F4 },
282 { NSF5FunctionKey, Qt::Key_F5 },
283 { NSF6FunctionKey, Qt::Key_F6 },
284 { NSF7FunctionKey, Qt::Key_F7 },
285 { NSF8FunctionKey, Qt::Key_F8 },
286 { NSF9FunctionKey, Qt::Key_F9 },
287 { NSF10FunctionKey, Qt::Key_F10 },
288 { NSF11FunctionKey, Qt::Key_F11 },
289 { NSF12FunctionKey, Qt::Key_F12 },
290 { NSF13FunctionKey, Qt::Key_F13 },
291 { NSF14FunctionKey, Qt::Key_F14 },
292 { NSF15FunctionKey, Qt::Key_F15 },
293 { NSF16FunctionKey, Qt::Key_F16 },
294 { NSF17FunctionKey, Qt::Key_F17 },
295 { NSF18FunctionKey, Qt::Key_F18 },
296 { NSF19FunctionKey, Qt::Key_F19 },
297 { NSF20FunctionKey, Qt::Key_F20 },
298 { NSF21FunctionKey, Qt::Key_F21 },
299 { NSF22FunctionKey, Qt::Key_F22 },
300 { NSF23FunctionKey, Qt::Key_F23 },
301 { NSF24FunctionKey, Qt::Key_F24 },
302 { NSF25FunctionKey, Qt::Key_F25 },
303 { NSF26FunctionKey, Qt::Key_F26 },
304 { NSF27FunctionKey, Qt::Key_F27 },
305 { NSF28FunctionKey, Qt::Key_F28 },
306 { NSF29FunctionKey, Qt::Key_F29 },
307 { NSF30FunctionKey, Qt::Key_F30 },
308 { NSF31FunctionKey, Qt::Key_F31 },
309 { NSF32FunctionKey, Qt::Key_F32 },
310 { NSF33FunctionKey, Qt::Key_F33 },
311 { NSF34FunctionKey, Qt::Key_F34 },
312 { NSF35FunctionKey, Qt::Key_F35 },
313 { NSInsertFunctionKey, Qt::Key_Insert },
314 { NSDeleteFunctionKey, Qt::Key_Delete },
315 { NSHomeFunctionKey, Qt::Key_Home },
316 { NSEndFunctionKey, Qt::Key_End },
317 { NSPageUpFunctionKey, Qt::Key_PageUp },
318 { NSPageDownFunctionKey, Qt::Key_PageDown },
319 { NSPrintScreenFunctionKey, Qt::Key_Print },
320 { NSScrollLockFunctionKey, Qt::Key_ScrollLock },
321 { NSPauseFunctionKey, Qt::Key_Pause },
322 { NSSysReqFunctionKey, Qt::Key_SysReq },
323 { NSMenuFunctionKey, Qt::Key_Menu },
324 { NSHelpFunctionKey, Qt::Key_Help },
327QChar QAppleKeyMapper::toCocoaKey(Qt::Key key)
330 if (key == Qt::Key_Return)
331 return char16_t(NSCarriageReturnCharacter);
332 if (key == Qt::Key_Backspace)
333 return char16_t(NSBackspaceCharacter);
335 Q_CONSTINIT
static QHash<Qt::Key,
char16_t> reverseCocoaKeys;
336 if (reverseCocoaKeys.isEmpty()) {
337 reverseCocoaKeys.reserve(cocoaKeys.size());
338 for (
auto it = cocoaKeys.begin(); it != cocoaKeys.end(); ++it)
339 reverseCocoaKeys.insert(it.value(), it.key());
342 return reverseCocoaKeys.value(key);
345Qt::Key QAppleKeyMapper::fromCocoaKey(QChar keyCode)
347 if (
auto key = cocoaKeys.value(keyCode.unicode()))
350 return Qt::Key(keyCode.toUpper().unicode());
355Qt::KeyboardModifiers QAppleKeyMapper::queryKeyboardModifiers()
const
357 return fromCocoaModifiers(NSEvent.modifierFlags);
360bool QAppleKeyMapper::updateKeyboard()
362 QCFType<TISInputSourceRef> source = TISCopyInputMethodKeyboardLayoutOverride();
364 source = TISCopyCurrentKeyboardInputSource();
366 if (m_keyboardMode != NullMode && source == m_currentInputSource)
370 m_currentInputSource = source;
371 m_keyboardKind = LMGetKbdType();
375 if (
auto data = CFDataRef(TISGetInputSourceProperty(source, kTISPropertyUnicodeKeyLayoutData))) {
376 const UCKeyboardLayout *uchrData =
reinterpret_cast<
const UCKeyboardLayout *>(CFDataGetBytePtr(data));
378 m_keyboardLayoutFormat = uchrData;
379 m_keyboardMode = UnicodeMode;
381 m_keyboardLayoutFormat =
nullptr;
382 m_keyboardMode = NullMode;
385 qCDebug(lcQpaKeyMapper) <<
"Updated keyboard to"
386 << QString::fromCFString(CFStringRef(TISGetInputSourceProperty(
387 m_currentInputSource, kTISPropertyLocalizedName)));
392static constexpr Qt::KeyboardModifiers modifierCombinations[] = {
396 Qt::ControlModifier | Qt::ShiftModifier,
398 Qt::AltModifier | Qt::ShiftModifier,
399 Qt::AltModifier | Qt::ControlModifier,
400 Qt::AltModifier | Qt::ShiftModifier | Qt::ControlModifier,
402 Qt::MetaModifier | Qt::ShiftModifier,
403 Qt::MetaModifier | Qt::ControlModifier,
404 Qt::MetaModifier | Qt::ControlModifier | Qt::ShiftModifier,
405 Qt::MetaModifier | Qt::AltModifier,
406 Qt::MetaModifier | Qt::AltModifier | Qt::ShiftModifier,
407 Qt::MetaModifier | Qt::AltModifier | Qt::ControlModifier,
408 Qt::MetaModifier | Qt::AltModifier | Qt::ShiftModifier | Qt::ControlModifier,
412
413
414
415const QAppleKeyMapper::KeyMap &QAppleKeyMapper::keyMapForKey(VirtualKeyCode virtualKey)
const
417 static_assert(
sizeof(modifierCombinations) /
sizeof(Qt::KeyboardModifiers) == kNumModifierCombinations);
419 const_cast<QAppleKeyMapper *>(
this)->updateKeyboard();
421 auto &keyMap = m_keyMap[virtualKey];
422 if (keyMap[Qt::NoModifier] != Qt::Key_unknown)
425 qCDebug(lcQpaKeyMapper,
"Updating key map for virtual key 0x%02x", (uint)virtualKey);
430 const bool canMapCocoaEvent = NSApp.currentEvent.type == NSEventTypeKeyDown;
432 if (!canMapCocoaEvent)
433 qCWarning(lcQpaKeyMapper) <<
"Could not map key to character for event" << NSApp.currentEvent;
435 for (
int i = 0; i < kNumModifierCombinations; ++i) {
436 Q_ASSERT(!i || keyMap[i] == 0);
438 auto qtModifiers = modifierCombinations[i];
439 auto carbonModifiers = toCarbonModifiers(qtModifiers);
440 const UInt32 modifierKeyState = (carbonModifiers >> 8) & 0xFF;
442 UInt32 deadKeyState = 0;
443 static const UniCharCount maxStringLength = 10;
444 static UniChar unicodeString[maxStringLength];
445 UniCharCount actualStringLength = 0;
446 OSStatus err = UCKeyTranslate(m_keyboardLayoutFormat, virtualKey,
447 kUCKeyActionDown, modifierKeyState, m_keyboardKind,
448 kUCKeyTranslateNoDeadKeysMask, &deadKeyState,
449 maxStringLength, &actualStringLength,
453 QChar carbonUnicodeKey;
454 if (err == noErr && actualStringLength)
455 carbonUnicodeKey = QChar(unicodeString[0]);
457 if (canMapCocoaEvent) {
461 auto cocoaModifiers = toCocoaModifiers(qtModifiers);
462 auto *charactersWithModifiers = [NSApp.currentEvent charactersByApplyingModifiers:cocoaModifiers];
464 QChar cocoaUnicodeKey;
465 if (charactersWithModifiers.length > 0)
466 cocoaUnicodeKey = QChar([charactersWithModifiers characterAtIndex:0]);
468 if (cocoaUnicodeKey != carbonUnicodeKey) {
469 qCWarning(lcQpaKeyMapper) <<
"Mismatch between Cocoa" << cocoaUnicodeKey
470 <<
"and Carbon" << carbonUnicodeKey <<
"for virtual key" << virtualKey
471 <<
"with" << qtModifiers;
475 int qtKey = toKeyCode(carbonUnicodeKey, virtualKey, qtModifiers);
476 if (qtKey == Qt::Key_unknown)
477 qtKey = carbonUnicodeKey.unicode();
481 qCDebug(lcQpaKeyMapper).verbosity(0) <<
"\t" << qtModifiers
482 <<
"+" << qUtf8Printable(QString::asprintf(
"0x%02x", virtualKey))
483 <<
"=" << qUtf8Printable(QString::asprintf(
"%d / 0x%02x /", qtKey, qtKey))
484 << QKeySequence(qtKey).toString();
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507QList<QKeyCombination> QAppleKeyMapper::possibleKeyCombinations(
const QKeyEvent *event)
const
509 QList<QKeyCombination> ret;
511 const auto nativeVirtualKey = event->nativeVirtualKey();
512 if (!nativeVirtualKey)
515 auto keyMap = keyMapForKey(nativeVirtualKey);
517 auto unmodifiedKey = keyMap[Qt::NoModifier];
518 Q_ASSERT(unmodifiedKey != Qt::Key_unknown);
520 auto eventModifiers = event->modifiers();
522 int startingModifierLayer = 0;
523 if (toCocoaModifiers(eventModifiers) & NSEventModifierFlagCommand) {
538 const int commandLayer = qApp->testAttribute(Qt::AA_MacDontSwapCtrlAndMeta) ? 8 : 2;
542 ret << QKeyCombination::fromCombined(
int(eventModifiers) +
int(keyMap[commandLayer]));
563 if (unmodifiedKey <= 0xff)
564 startingModifierLayer = 1;
568 for (
int i = startingModifierLayer; i < 15; ++i) {
569 auto keyAfterApplyingModifiers = keyMap[i];
570 if (!keyAfterApplyingModifiers)
575 auto candidateModifiers = modifierCombinations[i];
576 if ((eventModifiers & candidateModifiers) == candidateModifiers) {
579 auto additionalModifiers = eventModifiers & ~candidateModifiers;
581 auto keyCombination = QKeyCombination::fromCombined(
582 int(additionalModifiers) +
int(keyAfterApplyingModifiers));
587 const auto existingCombination = std::find_if(
588 ret.begin(), ret.end(), [&](
auto existingCombination) {
589 return existingCombination.key() == keyAfterApplyingModifiers;
592 if (existingCombination != ret.end()) {
605 auto existingModifiers = swapModifiersIfNeeded(existingCombination->keyboardModifiers());
606 auto replacementModifiers = swapModifiersIfNeeded(additionalModifiers);
607 if (replacementModifiers > existingModifiers)
608 *existingCombination = keyCombination;
611 ret << keyCombination;
623Qt::Key QAppleKeyMapper::fromNSString(Qt::KeyboardModifiers qtModifiers, NSString *characters,
624 NSString *charactersIgnoringModifiers, QString &text)
626 if ([characters isEqualToString:@
"\t"]) {
627 if (qtModifiers & Qt::ShiftModifier)
628 return Qt::Key_Backtab;
630 }
else if ([characters isEqualToString:@
"\r"]) {
631 if (qtModifiers & Qt::KeypadModifier)
632 return Qt::Key_Enter;
633 return Qt::Key_Return;
635 if ([characters length] != 0 || [charactersIgnoringModifiers length] != 0) {
637 if (((qtModifiers & Qt::MetaModifier) || (qtModifiers & Qt::AltModifier)) &&
638 ([charactersIgnoringModifiers length] != 0)) {
639 ch = QChar([charactersIgnoringModifiers characterAtIndex:0]);
640 }
else if ([characters length] != 0) {
641 ch = QChar([characters characterAtIndex:0]);
643 if (!(qtModifiers & (Qt::ControlModifier | Qt::MetaModifier)) &&
644 (ch.unicode() < 0xf700 || ch.unicode() > 0xf8ff)) {
645 text = QString::fromNSString(characters);
648 return Qt::Key(ch.toUpper().unicode());
650 return Qt::Key_unknown;
654API_AVAILABLE(ios(13.4)) Qt::Key QAppleKeyMapper::fromUIKitKey(NSString *keyCode)
656 static QHash<NSString *, Qt::Key> uiKitKeys = {
657 { UIKeyInputF1, Qt::Key_F1 },
658 { UIKeyInputF2, Qt::Key_F2 },
659 { UIKeyInputF3, Qt::Key_F3 },
660 { UIKeyInputF4, Qt::Key_F4 },
661 { UIKeyInputF5, Qt::Key_F5 },
662 { UIKeyInputF6, Qt::Key_F6 },
663 { UIKeyInputF7, Qt::Key_F7 },
664 { UIKeyInputF8, Qt::Key_F8 },
665 { UIKeyInputF9, Qt::Key_F9 },
666 { UIKeyInputF10, Qt::Key_F10 },
667 { UIKeyInputF11, Qt::Key_F11 },
668 { UIKeyInputF12, Qt::Key_F12 },
669 { UIKeyInputHome, Qt::Key_Home },
670 { UIKeyInputEnd, Qt::Key_End },
671 { UIKeyInputPageUp, Qt::Key_PageUp },
672 { UIKeyInputPageDown, Qt::Key_PageDown },
673 { UIKeyInputEscape, Qt::Key_Escape },
674 { UIKeyInputUpArrow, Qt::Key_Up },
675 { UIKeyInputDownArrow, Qt::Key_Down },
676 { UIKeyInputLeftArrow, Qt::Key_Left },
677 { UIKeyInputRightArrow, Qt::Key_Right }
680 if (
auto key = uiKitKeys.value(keyCode))
683 return Qt::Key_unknown;
686Qt::Key QAppleKeyMapper::fromUIKitKey(UIKeyboardHIDUsage usage)
688 static QHash<UIKeyboardHIDUsage, Qt::Key> modifierKeys = {
689 { UIKeyboardHIDUsageKeyboardCapsLock, Qt::Key_CapsLock },
690 { UIKeyboardHIDUsageKeyboardLeftAlt, Qt::Key_Alt },
691 { UIKeyboardHIDUsageKeyboardRightAlt, Qt::Key_Alt },
692 { UIKeyboardHIDUsageKeyboardLeftControl, Qt::Key_Control },
693 { UIKeyboardHIDUsageKeyboardRightControl, Qt::Key_Control },
694 { UIKeyboardHIDUsageKeyboardLeftShift, Qt::Key_Shift },
695 { UIKeyboardHIDUsageKeyboardRightShift, Qt::Key_Shift },
696 { UIKeyboardHIDUsageKeyboardLeftGUI, Qt::Key_Meta },
697 { UIKeyboardHIDUsageKeyboardRightGUI, Qt::Key_Meta },
700 if (
auto key = modifierKeys.value(usage))
701 return swapModifierIfNeeded(key);
703 return Qt::Key_unknown;
707 { UIKeyModifierShift, Qt::ShiftModifier },
708 { UIKeyModifierControl, Qt::ControlModifier },
709 { UIKeyModifierCommand, Qt::MetaModifier },
710 { UIKeyModifierAlternate, Qt::AltModifier },
711 { UIKeyModifierNumericPad, Qt::KeypadModifier }
714ulong QAppleKeyMapper::toUIKitModifiers(Qt::KeyboardModifiers qtModifiers)
716 qtModifiers = swapModifiersIfNeeded(qtModifiers);
718 ulong nativeModifiers = 0;
719 for (
const auto &[nativeModifier, qtModifier] : uiKitModifierMap) {
720 if (qtModifiers & qtModifier)
721 nativeModifiers |= nativeModifier;
724 return nativeModifiers;
727Qt::KeyboardModifiers QAppleKeyMapper::fromUIKitModifiers(ulong nativeModifiers)
729 Qt::KeyboardModifiers qtModifiers = Qt::NoModifier;
730 for (
const auto &[nativeModifier, qtModifier] : uiKitModifierMap) {
731 if (nativeModifiers & nativeModifier)
732 qtModifiers |= qtModifier;
735 return swapModifiersIfNeeded(qtModifiers);
Combined button and popup list for selecting options.
static constexpr std::tuple< ulong, Qt::KeyboardModifier > uiKitModifierMap[]
API_AVAILABLE(ios(13.4)) Qt
static QT_BEGIN_NAMESPACE Qt::KeyboardModifiers swapModifiersIfNeeded(const Qt::KeyboardModifiers modifiers)
static Qt::Key swapModifierIfNeeded(const Qt::Key key)