diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2018-10-05 16:29:16 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2018-10-05 14:35:44 +0000 |
commit | 6eb44ec4831f58e7658efbb0b9f0813629b9cdea (patch) | |
tree | 38d479a2c1c64b32d41bf0370298d3fdc9c3be73 /qxt-mini/qxtglobalshortcut_mac.cpp | |
parent | 388e797a8e70ea93d08b20b522c4029f8741fa34 (diff) |
qxt-mini: try fix osx shortcut snafu
Diffstat (limited to 'qxt-mini/qxtglobalshortcut_mac.cpp')
-rw-r--r-- | qxt-mini/qxtglobalshortcut_mac.cpp | 208 |
1 files changed, 93 insertions, 115 deletions
diff --git a/qxt-mini/qxtglobalshortcut_mac.cpp b/qxt-mini/qxtglobalshortcut_mac.cpp index d7cd7f84..5f55f579 100644 --- a/qxt-mini/qxtglobalshortcut_mac.cpp +++ b/qxt-mini/qxtglobalshortcut_mac.cpp @@ -43,132 +43,104 @@ typedef QPair<uint, uint> Identifier; static QMap<quint32, EventHotKeyRef> keyRefs; static QHash<Identifier, quint32> keyIDs; static quint32 hotKeySerial = 0; -static bool qxt_mac_handler_installed = false; OSStatus qxt_mac_handle_hot_key(EventHandlerCallRef nextHandler, EventRef event, void* data) { Q_UNUSED(nextHandler); Q_UNUSED(data); - if (GetEventClass(event) == kEventClassKeyboard && GetEventKind(event) == kEventHotKeyPressed) + if (GetEventClass(event) == kEventClassKeyboard) { + int kind = GetEventKind(event); + bool is_down; + + if (kind == kEventHotKeyPressed) + is_down = true; + else if (kind == kEventHotKeyReleased) + is_down = false; + else + return noErr; + EventHotKeyID keyID; GetEventParameter(event, kEventParamDirectObject, typeEventHotKeyID, NULL, sizeof(keyID), NULL, &keyID); Identifier id = keyIDs.key(keyID.id); if(id != Identifier()) - QxtGlobalShortcutPrivate::activateShortcut(id.second, id.first, true); + QxtGlobalShortcutPrivate::activateShortcut(id.second, id.first, is_down); } return noErr; } +// Constants found in NSEvent.h from AppKit.framework +// cf. https://stackoverflow.com/a/16125341 +// cf. https://web.archive.org/web/20100501161453/http://www.classicteck.com/rbarticles/mackeyboard.php + +static struct { + Qt::Key qt_key; + unsigned native_key; +} const key_list[] = { + { Qt::Key_Escape , 0x35 }, + { Qt::Key_Return , 0x24 }, + { Qt::Key_Return , 0x4c }, + { Qt::Key_Tab , 0x30 }, + { Qt::Key_Space , 0x31 }, + { Qt::Key_Delete , 0x33 }, + { Qt::Key_F17 , 0x40 }, + { Qt::Key_VolumeUp , 0x48 }, + { Qt::Key_VolumeDown , 0x49 }, + { Qt::Key_Mute , 0x4A }, + { Qt::Key_F1 , 0x7A }, + { Qt::Key_F2 , 0x78 }, + { Qt::Key_F3 , 0x63 }, + { Qt::Key_F4 , 0x76 }, + { Qt::Key_F5 , 0x60 }, + { Qt::Key_F6 , 0x61 }, + { Qt::Key_F7 , 0x62 }, + { Qt::Key_F8 , 0x64 }, + { Qt::Key_F9 , 0x65 }, + { Qt::Key_F10 , 0x6D }, + { Qt::Key_F11 , 0x67 }, + { Qt::Key_F12 , 0x6F }, + { Qt::Key_Help , 0x72 }, + { Qt::Key_PageUp , 0x74 }, + { Qt::Key_PageDown , 0x79 }, + { Qt::Key_Delete , 0x33 }, + { Qt::Key_Backspace , 0x75 }, + { Qt::Key_Home , 0x73 }, + { Qt::Key_End , 0x77 }, + { Qt::Key_LeftArrow , 0x7B }, + { Qt::Key_RightArrow , 0x7C }, + { Qt::Key_DownArrow , 0x7D }, + { Qt::Key_UpArrow , 0x7E }, +}; + +static struct modifiers { + Qt::KeyboardModifiers qt_mods; + unsigned native_mods; +} const modifier_list[] = { + { Qt::ShiftModifier, 56 }, + { Qt::ControlModifier, 59 }, + { Qt::AltModifier, 55 }, + { Qt::MetaModifier, 58 }, + //{ Qt::KeypadModifier, kEventKeyModifierNumLockMask }, +}; + quint32 QxtGlobalShortcutPrivate::nativeModifiers(Qt::KeyboardModifiers modifiers) { quint32 native = 0; - if (modifiers & Qt::ShiftModifier) - native |= shiftKey; - if (modifiers & Qt::ControlModifier) - native |= cmdKey; - if (modifiers & Qt::AltModifier) - native |= optionKey; - if (modifiers & Qt::MetaModifier) - native |= controlKey; - if (modifiers & Qt::KeypadModifier) - native |= kEventKeyModifierNumLockMask; + + for (const auto& m : modifiers) + if (modifiers & m.qt_mods) + native |= m.native_mods; + return native; } quint32 QxtGlobalShortcutPrivate::nativeKeycode(Qt::Key keys) { UTF16Char ch; - // Constants found in NSEvent.h from AppKit.framework - switch (keys) - { - case Qt::Key_Return: - return kVK_Return; - case Qt::Key_Enter: - return kVK_ANSI_KeypadEnter; - case Qt::Key_Tab: - return kVK_Tab; - case Qt::Key_Space: - return kVK_Space; - case Qt::Key_Backspace: - return kVK_Delete; - case Qt::Key_Control: - return kVK_Command; - case Qt::Key_Shift: - return kVK_Shift; - case Qt::Key_CapsLock: - return kVK_CapsLock; - case Qt::Key_Option: - return kVK_Option; - case Qt::Key_Meta: - return kVK_Control; - case Qt::Key_F17: - return kVK_F17; - case Qt::Key_VolumeUp: - return kVK_VolumeUp; - case Qt::Key_VolumeDown: - return kVK_VolumeDown; - case Qt::Key_F18: - return kVK_F18; - case Qt::Key_F19: - return kVK_F19; - case Qt::Key_F20: - return kVK_F20; - case Qt::Key_F5: - return kVK_F5; - case Qt::Key_F6: - return kVK_F6; - case Qt::Key_F7: - return kVK_F7; - case Qt::Key_F3: - return kVK_F3; - case Qt::Key_F8: - return kVK_F8; - case Qt::Key_F9: - return kVK_F9; - case Qt::Key_F11: - return kVK_F11; - case Qt::Key_F13: - return kVK_F13; - case Qt::Key_F16: - return kVK_F16; - case Qt::Key_F14: - return kVK_F14; - case Qt::Key_F10: - return kVK_F10; - case Qt::Key_F12: - return kVK_F12; - case Qt::Key_F15: - return kVK_F15; - case Qt::Key_Help: - return kVK_Help; - case Qt::Key_Home: - return kVK_Home; - case Qt::Key_PageUp: - return kVK_PageUp; - case Qt::Key_Delete: - return kVK_ForwardDelete; - case Qt::Key_F4: - return kVK_F4; - case Qt::Key_End: - return kVK_End; - case Qt::Key_F2: - return kVK_F2; - case Qt::Key_PageDown: - return kVK_PageDown; - case Qt::Key_F1: - return kVK_F1; - case Qt::Key_Left: - return kVK_LeftArrow; - case Qt::Key_Right: - return kVK_RightArrow; - case Qt::Key_Down: - return kVK_DownArrow; - case Qt::Key_Up: - return kVK_UpArrow; - default: - ; - } + + for (k const& : key_list) + if (k.qt_key == keys) + return k.native_key; if (keys == Qt::Key_Escape) ch = 27; else if (keys == Qt::Key_Return) ch = 13; @@ -228,16 +200,21 @@ quint32 QxtGlobalShortcutPrivate::nativeKeycode(Qt::Key keys) return 0; } +static bool register_event_handler() +{ + EventTypeSpec specs[2] = { + { kEventClassKeyboard, kEventHotKeyPressed }, + { kEventClassKeyboard, kEventHotKeyReleased }, + }; + + (void)InstallApplicationEventHandler(&qxt_mac_handle_hot_key, 2, &t, NULL, NULL); + + return true; +} + bool QxtGlobalShortcutPrivate::registerShortcut(quint32 nativeKey, quint32 nativeMods) { - if (!qxt_mac_handler_installed) - { - qxt_mac_handler_installed = true; - EventTypeSpec t; - t.eventClass = kEventClassKeyboard; - t.eventKind = kEventHotKeyPressed; - InstallApplicationEventHandler(&qxt_mac_handle_hot_key, 1, &t, NULL, NULL); - } + static const bool once = register_event_handler(); EventHotKeyID keyID; keyID.signature = 'cute'; @@ -255,15 +232,16 @@ bool QxtGlobalShortcutPrivate::registerShortcut(quint32 nativeKey, quint32 nativ bool QxtGlobalShortcutPrivate::unregisterShortcut(quint32 nativeKey, quint32 nativeMods) { - Identifier id(nativeMods, nativeKey); - if (!keyIDs.contains(id)) return false; + Identifier id{nativeMods, nativeKey}; + if (!keyIDs.contains(id)) + return false; EventHotKeyRef ref = keyRefs.take(keyIDs[id]); keyIDs.remove(id); return !UnregisterEventHotKey(ref); } -bool QxtGlobalShortcutPrivate::nativeEventFilter(const QByteArray & eventType, - void *message, long *result) + +bool QxtGlobalShortcutPrivate::nativeEventFilter(const QByteArray & eventType, void *message, long *result) { return false; } |