Modeling CMD + Option + D in Cocoa

I need to simulate the CMD + Option + D button at the same time. I have done all sorts and the best way I've seen is to do this:

CGEventSourceRef src = CGEventSourceCreate(kCGEventSourceStateHIDSystemState); CGEventRef cmdd = CGEventCreateKeyboardEvent(src, kVK_Command, true); CGEventRef cmdu = CGEventCreateKeyboardEvent(src, kVK_Command, false); CGEventRef optd = CGEventCreateKeyboardEvent(src, kVK_Option, true); CGEventRef optu = CGEventCreateKeyboardEvent(src, kVK_Option, false); CGEventRef dd = CGEventCreateKeyboardEvent(src, kVK_ANSI_D, true); CGEventRef du = CGEventCreateKeyboardEvent(src, kVK_ANSI_D, false); CGEventSetFlags(dd, kCGEventFlagMaskCommand); //NO idea why this is here. CGEventSetFlags(du, kCGEventFlagMaskCommand); //NO idea why this is here. CGEventTapLocation loc = kCGHIDEventTap; // kCGSessionEventTap also works CGEventPost(loc, cmdd); //Cmd down CGEventPost(loc, optd); //Option down CGEventPost(loc, dd); //D down CGEventPost(loc, cmdu); //Cmd up CGEventPost(loc, optu); //Option up CGEventPost(loc, du); //D up CFRelease(cmdd); CFRelease(cmdu); CFRelease(optd); CFRelease(optu); CFRelease(dd); CFRelease(du); CFRelease(src); 

However, this does not switch the dock, as when using my keyboard? Why is this? What am I doing wrong? I imported Carbon.h, so it seems like this should work?

+2
source share
1 answer

Have you tried this?

 // as before ... CGEventSetFlags(dd, kCGEventFlagMaskCommand ^ kCGEventFlagMaskAlternate); CGEventSetFlags(du, kCGEventFlagMaskCommand ^ kCGEventFlagMaskAlternate); // continues... 

I think you need to set both command and alternative (option key) for event D.

+2
source

Source: https://habr.com/ru/post/1485904/


All Articles