From 268f09160d0881f55646e4b44def5e40fa40146a Mon Sep 17 00:00:00 2001 From: Weslly Date: Sat, 25 Mar 2017 18:42:52 -0300 Subject: [PATCH] Add command modifier support for AutoTypePlatformMac::sendKey --- src/autotype/mac/AutoTypeMac.cpp | 40 ++++++++++++++------------------ src/autotype/mac/AutoTypeMac.h | 2 +- 2 files changed, 18 insertions(+), 24 deletions(-) diff --git a/src/autotype/mac/AutoTypeMac.cpp b/src/autotype/mac/AutoTypeMac.cpp index 57e2e9e15..122bffeaa 100644 --- a/src/autotype/mac/AutoTypeMac.cpp +++ b/src/autotype/mac/AutoTypeMac.cpp @@ -201,15 +201,25 @@ void AutoTypePlatformMac::sendChar(const QChar& ch, bool isKeyDown) // Send key code to active window // see: Quartz Event Services // -void AutoTypePlatformMac::sendKey(Qt::Key key, bool isKeyDown) +void AutoTypePlatformMac::sendKey(Qt::Key key, bool isKeyDown, bool isCommand = false) { - uint16 keyCode = qtToNativeKeyCode(key); + uint16 keyCode = 0; + + if (isCommand && key == Qt::Key_Any) { + keyCode = kVK_Command; + } else { + keyCode = qtToNativeKeyCode(key); + } + if (keyCode == INVALID_KEYCODE) { return; } CGEventRef keyEvent = ::CGEventCreateKeyboardEvent(nullptr, keyCode, isKeyDown); if (keyEvent != nullptr) { + if (isCommand && isKeyDown) { + ::CGEventSetFlags(keyEvent, kCGEventFlagMaskCommand); + } ::CGEventPost(kCGSessionEventTap, keyEvent); ::CFRelease(keyEvent); } @@ -489,29 +499,13 @@ void AutoTypeExecutorMac::execKey(AutoTypeKey* action) usleep(25 * 1000); } -void execKeyPress(uint16 keyCode, bool isKeyDown, bool modifier = false) -{ - CGEventRef keyEvent = ::CGEventCreateKeyboardEvent(nullptr, keyCode, isKeyDown); - if (keyEvent != nullptr) { - if (modifier) { - ::CGEventSetFlags(keyEvent, kCGEventFlagMaskCommand); - } - - ::CGEventPost(kCGSessionEventTap, keyEvent); - ::CFRelease(keyEvent); - } - usleep(25 * 1000); -} - void AutoTypeExecutorMac::execClearField(AutoTypeClearField* action = nullptr) { Q_UNUSED(action); - - execKeyPress(kVK_ANSI_A, true, true); - execKeyPress(kVK_ANSI_A, false); - execKeyPress(kVK_Command, false); - execKeyPress(kVK_Delete, true); - execKeyPress(kVK_Delete, false); - + m_platform->sendKey(Qt::Key_A, true, true); + m_platform->sendKey(Qt::Key_A, false); + m_platform->sendKey(Qt::Key_Any, false, true); + m_platform->sendKey(Qt::Key_Backspace, true); + m_platform->sendKey(Qt::Key_Backspace, false); usleep(25 * 1000); } diff --git a/src/autotype/mac/AutoTypeMac.h b/src/autotype/mac/AutoTypeMac.h index c2dde398a..86b01826b 100644 --- a/src/autotype/mac/AutoTypeMac.h +++ b/src/autotype/mac/AutoTypeMac.h @@ -49,7 +49,7 @@ public: bool raiseOwnWindow() override; void sendChar(const QChar& ch, bool isKeyDown); - void sendKey(Qt::Key key, bool isKeyDown); + void sendKey(Qt::Key key, bool isKeyDown, bool isCommand); signals: void globalShortcutTriggered();