title: CMD+Option+D simulation in Cocoa
body: <p>I need to simulate a CMD+Option+D key press simultaneously. I've done all kind of looking and the best way so far I've seen is to do this:</p>
  
  <pre><code>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);
  </code></pre>
  
  <p>However, doing this does not toggle the dock like it does when I use my keyboard? Why is this? What am I doing wrong? I imported Carbon.h, so it seems like this should work?</p>