I've attached a MBA to an iMac using a thunderbolt cable. Pressing CMD+F2 on the iMac enables the target display mode to use the iMac as display for the MBA. Does anyone have information how to trigger that event programmatically?
My first approach was to send a CGEventPost
to kCGHIDEventTap
CGEventRef f2CommandDown = CGEventCreateKeyboardEvent(src, (CGKeyCode)120, YES);
CGEventSetFlags(f2CommandDown, kCGEventFlagMaskCommand);
CGEventRef f2CommandUp = CGEventCreateKeyboardEvent(src, (CGKeyCode)120, NO);
CGEventPost(kCGHIDEventTap, f2CommandDown);
CGEventPost(kCGHIDEventTap, f2CommandUp);
That doesn't work. All it does is an error "beep". (tried running as root user too). I think, kCGHIDEventTap
is just the wrong target and CMD+F2 might live in a higher level of the OS (aka. "somewhere")
Running some key-event capturing code doesn't show anything for CMD+F2.
Does anyone have a hint? Thanks in advance!
Choose Apple menu > System Preferences, then click Keyboard. If ”Use F1, F2, etc. keys as standard function keys” is selected, target display mode uses Command-Fn-F2 instead of Command-F2. It might also help to use the keyboard that came with your iMac.
The F2 key is a function key found at the top of almost all computer keyboards. The key is most often used in Microsoft Windows to rename a highlighted file or icon.
If Command-F2 Doesn't WorkMake sure you're pressing Command-F2 on the keyboard that's connected to the iMac you want to use as a display. In Keyboard System Preferences, if the checkbox is enabled for Use all F1, F2, etc. keys as standard functions keys, the key combination changes to Command-Fn-F2.
Target Display Mode was an extremely popular feature that allowed older macs to be used as external monitors for newer macs. However, Apple killed off Target Display Mode a few years back because it required too many Thunderbolt ports on newer model macs.
I've started a project that does this, namely monitor the iMac and automatically trigger target display mode and toggle off bluetooth when a Macbook is connected. You can download it from https://github.com/duanefields/VirtualKVM. I'm using AppleScript to trigger the keys.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With