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?
我使用雷电电缆将MBA连接到iMac。按iMac上的CMD + F2可使目标显示模式使用iMac作为MBA的显示。有没有人有关于如何以编程方式触发该事件的信息?
My first approach was to send a CGEventPost
to kCGHIDEventTap
我的第一个方法是将CGEventPost发送到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")
这不起作用。它只是一个错误“嘟嘟”。 (尝试以root用户身份运行)。我认为,kCGHIDEventTap只是错误的目标而CMD + F2可能存在于操作系统的更高级别(又名“某处”)
Running some key-event capturing code doesn't show anything for CMD+F2.
运行一些键事件捕获代码不会显示CMD + F2的任何内容。
Does anyone have a hint? Thanks in advance!
有人有提示吗?提前致谢!
3 个解决方案
#1
3
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.
我已经启动了一个项目,即监控iMac并自动触发目标显示模式,并在连接Macbook时关闭蓝牙。您可以从https://github.com/duanefields/VirtualKVM下载它。我正在使用AppleScript触发键。
#2
0
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
#3
0
Actually you can do this pretty easily without a program, using osascript.
实际上,如果没有程序,使用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.
如果你还想使用单个蓝牙键盘和触控板,那么你可以通过使用blueutil暂时禁用imac上的蓝牙来将它们切换到macbook,这样macbook就可以抓住键盘和触控板。每当您想要退出目标显示模式时,只需关闭我的Macbook上的蓝牙并等待几秒钟,以便imac重新连接到键盘和触控板。
On your imac, put the script below in a file ~/bin/target-display-mode, and run `chmod +x ~/bin/target-display-mode
在你的imac上,将下面的脚本放在〜/ bin / target-display-mode文件中,运行`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).
然后在您的imac上,在术语窗口中,将target-display-mode作为命令运行。如果你的macbook上启用了蓝牙,并且它已经知道你的键盘和触控板,那么它将连接到它们。或者打开蓝牙首选项并找到每个设备并“连接”(使用macbook的内置键盘和触控板)。
#! /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.
请注意,脚本转动等待60秒,然后在imac上重新打开蓝牙。没有其他键盘或硬接线鼠标非常重要。如果蓝牙仍然关闭,您将无法在不使用ssh或重新启动的情况下重新连接它们。
#1
3
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.
我已经启动了一个项目,即监控iMac并自动触发目标显示模式,并在连接Macbook时关闭蓝牙。您可以从https://github.com/duanefields/VirtualKVM下载它。我正在使用AppleScript触发键。
#2
0
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
#3
0
Actually you can do this pretty easily without a program, using osascript.
实际上,如果没有程序,使用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.
如果你还想使用单个蓝牙键盘和触控板,那么你可以通过使用blueutil暂时禁用imac上的蓝牙来将它们切换到macbook,这样macbook就可以抓住键盘和触控板。每当您想要退出目标显示模式时,只需关闭我的Macbook上的蓝牙并等待几秒钟,以便imac重新连接到键盘和触控板。
On your imac, put the script below in a file ~/bin/target-display-mode, and run `chmod +x ~/bin/target-display-mode
在你的imac上,将下面的脚本放在〜/ bin / target-display-mode文件中,运行`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).
然后在您的imac上,在术语窗口中,将target-display-mode作为命令运行。如果你的macbook上启用了蓝牙,并且它已经知道你的键盘和触控板,那么它将连接到它们。或者打开蓝牙首选项并找到每个设备并“连接”(使用macbook的内置键盘和触控板)。
#! /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.
请注意,脚本转动等待60秒,然后在imac上重新打开蓝牙。没有其他键盘或硬接线鼠标非常重要。如果蓝牙仍然关闭,您将无法在不使用ssh或重新启动的情况下重新连接它们。