const CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateCombinedSessionState); CGEventRef d; CGEventRef u;
uint64_t flags = 0; if (SendKeys::key_map_.count(lower_string)) { auto vk = SendKeys::key_map_.at(lower_string); d = CGEventCreateKeyboardEvent(source, vk, true); u = CGEventCreateKeyboardEvent(source, vk, false); if (control) flags |= kCGEventFlagMaskCommand; if (alt) flags |= kCGEventFlagMaskAlternate; if (shift) flags |= kCGEventFlagMaskShift; if (flags != UINT64_C(0)) { CGEventSetFlags(d, static_cast<CGEventFlags>(flags)); CGEventSetFlags(u, static_cast<CGEventFlags>(flags)); } } else { const std::wstring utf16str{utf8_to_utf16(key)}; const UniChar key_character = utf16str[0]; d = CGEventCreateKeyboardEvent(source, 0, true); u = CGEventCreateKeyboardEvent(source, 0, false); CGEventKeyboardSetUnicodeString(d, 1, &key_character); CGEventKeyboardSetUnicodeString(u, 1, &key_character); flags = CGEventGetFlags(d); //in case KeyCode has associated flag if (control) flags |= kCGEventFlagMaskCommand; if (alt) flags |= kCGEventFlagMaskAlternate; if (shift) flags |= kCGEventFlagMaskShift; if (flags != UINT64_C(0)) { CGEventSetFlags(d, static_cast<CGEventFlags>(flags)); CGEventSetFlags(u, static_cast<CGEventFlags>(flags)); } } CGEventRef cmdd = CGEventCreateKeyboardEvent(source, 0x37, true); CGEventRef cmdu = CGEventCreateKeyboardEvent(source, 0x37, false); { //restrict scope for mutex lock std::lock_guard< decltype(mutex_sending_) > lock(mutex_sending_); if (flags & kCGEventFlagMaskCommand) { CGEventPost(kCGHIDEventTap, cmdd); std::this_thread::sleep_for(std::chrono::milliseconds(30)); } CGEventPost(kCGHIDEventTap, d); std::this_thread::sleep_for(std::chrono::milliseconds(30)); CGEventPost(kCGHIDEventTap, u); if (flags & kCGEventFlagMaskCommand) { std::this_thread::sleep_for(std::chrono::milliseconds(30)); CGEventPost(kCGHIDEventTap, cmdu); } }
CFRelease(d); CFRelease(u); CFRelease(cmdd); CFRelease(cmdu); CFRelease(source);
2016-06-19 20:43:59.132 MIDI2LR[12819:12321419] Failed to get CharCodes from EventRef (-9870)
2016-06-19 20:43:59.135 MIDI2LR[12819:12321419] (
0 CoreFoundation 0x00007fff8d18e4f2 __exceptionPreprocess + 178
1 libobjc.A.dylib 0x00007fff89f9773c objc_exception_throw + 48
2 CoreFoundation 0x00007fff8d1f54bd +[NSException raise:format:] + 205
3 AppKit 0x00007fff8374a01f -[NSEvent _matchesKeyEquivalent:modifierMask:] + 315
4 AppKit 0x00007fff83798545 -[NSWindow _cancelActionIfCmdDot:] + 35
5 AppKit 0x00007fff837982df -[NSWindow _processKeyboardUIKey:] + 80
6 AppKit 0x00007fff8368664c -[NSWindow _handleKeyDownEvent:] + 325
7 AppKit 0x00007fff83cb0b15 -[NSWindow _reallySendEvent:isDelayedEvent:] + 2108
8 AppKit 0x00007fff836ef539 -[NSWindow sendEvent:] + 517
9 AppKit 0x00007fff8367016a -[NSApplication sendEvent:] + 4382
10 AppKit 0x00007fff834d6df2 -[NSApplication run] + 796
11 MIDI2LR 0x0000000100065cfc _ZN4juce19JUCEApplicationBase4mainEv + 188
12 MIDI2LR 0x0000000100065c23 _ZN4juce19JUCEApplicationBase4mainEiPPKc + 83
13 libdyld.dylib 0x00007fff882a55ad start + 1
14 ??? 0x0000000000000003 0x0 + 3
)
const CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateCombinedSessionState); const CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateHIDSystemState); const CGEventSourceRef source = nullptr;
const CGEventSourceRef source = 0;Could you try
const CGEventSourceRef source = 0;
On Tuesday, June 21, 2016 at 6:00:40 PM UTC-7, Christopher Herrmann wrote:
First change had no effect. Second one crashes MIDI2LR....
When I comment out the cmdd and cmdu events Lightroom fails to respond. Uncommented though works as planned.
here is the function, NOTE Sendkeys.cpp still has existing code above line 172 that I didn't touch.
void SendKeys::SendKeyDownUp(const std::string& key, bool alt, bool control, bool shift) const {
CGEventSourceRef src =
CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
//CGEventRef cmdd = CGEventCreateKeyboardEvent(src, 0x37, true);
//CGEventRef cmdu = CGEventCreateKeyboardEvent(src, 0x37, false);
CGEventRef akeyd = CGEventCreateKeyboardEvent(src, (CGKeyCode) 0, true);
CGEventRef akeyu = CGEventCreateKeyboardEvent(src, (CGKeyCode) 0, false);
CGEventSetFlags(akeyd, kCGEventFlagMaskCommand);
CGEventSetFlags(akeyu, kCGEventFlagMaskCommand);
CGEventTapLocation loc = kCGHIDEventTap; // kCGSessionEventTap also works
//CGEventPost(loc, cmdd);
CGEventPost(loc, akeyd);
CGEventPost(loc, akeyu);
//CGEventPost(loc, cmdu);
//CFRelease(cmdd);
//CFRelease(cmdu);
CFRelease(akeyd);
CFRelease(akeyu);
CFRelease(src);
osascript -e 'id of app "Lightroom"'then substitute it for "com.adobe.Lightroom6" (twice) in the following code, and use it for the sendkey app. Tell XCode to compile SendKeys.cpp as Objective-C++. If this works, you should be able to send the command-a to Lightroom even when it is not in the foreground. Also, please tell me what the bundleid was for your lightroom and which version (CC or 6) you're using. Thanks. CGEventSourceRef src = CGEventSourceCreate(kCGEventSourceStateHIDSystemState); CGEventRef cmdd = CGEventCreateKeyboardEvent(src, 0x37, true); CGEventRef cmdu = CGEventCreateKeyboardEvent(src, 0x37, false); CGEventRef akeyd = CGEventCreateKeyboardEvent(src, (CGKeyCode) 0, true); CGEventRef akeyu = CGEventCreateKeyboardEvent(src, (CGKeyCode) 0, false);
CGEventSetFlags(akeyd, kCGEventFlagMaskCommand); CGEventSetFlags(akeyu, kCGEventFlagMaskCommand);
CGEventTapLocation loc = kCGHIDEventTap; // kCGSessionEventTap also works OSStatus err{1}; ProcessSerialNumber psn;if ([[NSRunningApplication runningApplicationsWithBundleIdentifier:@"com.adobe.Lightroom6"] count]) { // get pid pid_t pid = [(NSRunningApplication*)[[NSRunningApplication runningApplicationsWithBundleIdentifier:@"com.adobe.Lightroom6"] objectAtIndex:0] processIdentifier];
// get PSN err = GetProcessForPID(pid, &psn);}if (err == noErr) { CGEventPostToPSN(&psn, cmdd); CGEventPostToPSN(&psn, akeyd); CGEventPostToPSN(&psn, akeyu); CGEventPostToPSN(&psn, cmdu);}else{ CGEventPost(loc, cmdd); CGEventPost(loc, akeyd); CGEventPost(loc, akeyu); CGEventPost(loc, cmdu);}
CFRelease(cmdd); CFRelease(cmdu); CFRelease(akeyd); CFRelease(akeyu); CFRelease(src);CGEventPost(loc, cmdd);
CGEventPost(loc, d);
CGEventPost(loc, u);
CGEventPost(loc, cmdu);CGPostKeyboardEvent( (CGCharCode)0, (CGKeyCode)55, true );
CGEventPost(loc, d);
CGEventPost(loc, u);
CGPostKeyboardEvent( (CGCharCode)0, (CGKeyCode)55, false );CGEventPost(loc, cmdd);
CGEventPost(loc, d);
CGEventPost(loc, u);
CGEventPost(loc, cmdu);CGEventRef cmdd = CGEventCreateKeyboardEvent(source, kVK_Command, true);
CGEventRef cmdu = CGEventCreateKeyboardEvent(source, kVK_Command, false);CGEventRef cmdd = CGEventCreateKeyboardEvent(nullptr, kVK_Command, true);
CGEventRef cmdu = CGEventCreateKeyboardEvent(nullptr, kVK_Command, false);