Reputation: 360
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!
Upvotes: 8
Views: 6835
Reputation: 195
Actually you can do this pretty easily without a program, using osascript.
osascript -e 'tell application "System Events" to key code 144 using command down'
But it won't do it automatically when you plug in the cable.
If you want to also use a single bluetooth keyboard and trackpad, then you can switch them over to the macbook by using blueutil to temporarily disable bluetooth on the imac so the macbook can grab the keyboard and trackpad. Whenever you want to exit target display mode, just turn off bluetooth on my macbook and wait a few seconds for the imac to reconnect to the keyboard and trackpad.
On your imac, put the script below in a file ~/bin/target-display-mode, and run `chmod +x ~/bin/target-display-mode
Then on your imac, in a term window, run target-display-mode as a command. If bluetooth is enabled on your macbook, and it already knows your keyboard and trackpad, then it will connect to them. Or open bluetooth preferences and find each device and "connect" (using the macbook's built-in keyboard and trackpad).
#! /usr/bin/env bash
# Enter target-display mode with a macbook connected by cable;
# then, temporarily turn off bluetooth so the macbook can the
# bluetooth keyboard, trackpad and other devices that are currently
# connected to the imac.
#
# Later, turn bluetooth back on so the imac can later reconnect to it's
# bluetooth devices.
#
# To exit target display mode, turn off bluetooth on the macbook and
# disconnect the cable. After a few seconds, the imac will reconnect to
# the keyboard and trackpad.
#
osascript -e 'tell application "System Events" to key code 144 using command down'
sleep 5
(
/usr/local/bin/blueutil off
sleep 60
/usr/local/bin/blueutil on
) &
Notice that the script turn waits 60 seconds and then turns bluetooth back on on the imac. That's really important you don't have another keyboard or a hard-wired mouse. If bluetooth remained off, you wouldn't be able to reconnect them without using ssh, or rebooting.
Upvotes: 3
Reputation: 1341
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.
Upvotes: 4
Reputation: 1341
Was wondering if you ever figured this out. The only solution I've seen is running a full screen window and triggering key presses like this
http://bogner.sh/2013/07/os-x-how-to-use-an-imac-as-monitor/#comment-50925
Upvotes: 0