Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically enter target display mode?

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!

like image 857
Boris Avatar asked Feb 21 '13 19:02

Boris


People also ask

Is there another way to enter Target display Mode?

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.

What is Command-F2 on a Windows keyboard?

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.

Why does Command-F2 not work on iMac?

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.

Why did they get rid of Target display Mode?

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.


1 Answers

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.

like image 195
Duane Fields Avatar answered Nov 13 '22 08:11

Duane Fields