Sending key via ssh or using mouse to activate Target Display Mode?

11

4

Is it possible to send a keystroke via SSH?  I want to be able to send ⌘ (CMD)+F2 via ssh from/to Mac OS X.  If that's not possible, a mouse solution would also be good.

The reason I need this is because I have only one keyboard (but two mice).  I use my iMac as external display. To activate the target display mode on the iMac I must connect the Bluetooth keyboard every time, hit CMD+F2, then try to get my laptop to connect to the keyboard.

Sending CMD+F2 via ssh or being able to use my mouse on the original iMac would save me loads of trouble!

iDev247

Posted 2013-01-23T23:58:44.820

Reputation: 267

Answers

3

Untested, but I might consider creating an applescript in ~/bin/command_f2 on the iMac with the following contents:

tell application "System Events" to keystroke "F2" using {command down}

and then from your laptop type:

ssh me@imac '~/bin/command_f2'

To get more help with this question, consider changing the title to something like "Sending keystrokes via ssh" because "keys" could mean confused with ssh keys.

Good luck.

John Schmitt

Posted 2013-01-23T23:58:44.820

Reputation: 427

1

On the right path. What I tried was osascript -e 'tell application "System Events" to key code 120 using command down';. It generally works and can send keys but when it comes to CMD+F2 it doesn't appear to work/enable Target Display Mode. Is the command correct? Is there any other way to enable Target Display mode with mouse or command? Found key codes here

– iDev247 – 2013-01-24T00:27:42.063

I was able to do it with http://www.keyboardmaestro.com/main/... I wonder what it sends as command to make it work.

– iDev247 – 2013-01-24T00:51:15.620

I don't have an apple keyboard, could you share your macro if possible? Recording Cmd-F2 using my filco does not switcj. – Florian Doyon – 2013-11-09T15:04:35.917

15

Sorry, John Schmitt, your untested solution doesn't work, but you were on the right track. This one's tested, and it works.

The right command is (if you embed Applescripts in shell scripts, which is how I prefer doing it):

osascript -e 'tell application "System Events" to key code 144 using command down'

Save that, for example, as ~/bin/tdm.sh, on the the target display machine.

Then, from the primary machine, issue this in Terminal:

ssh username@target-display-machine "~/bin/tdm.sh"

There are other ways to save and invoke the Applescript command. I find it more convenient and consistent to embed them in shell scripts -- my particular preference, since I do a lot of shell scripts anyway.

Jose

Posted 2013-01-23T23:58:44.820

Reputation: 151

1This isn't working for me, on a 27" iMac that was happily entering TDM via cmd-f2 on a real Apple keyboard yesterday, but isn't anymore today. Anyone know of a way I can force it from the firmware? I don't need/want to boot MacOS at all on this machine. – Robert Atkins – 2014-07-17T09:26:17.313

1This works for me but only if a physical keyboard is actually connected to it via USB or bluetooth. Is it not possible to do this without it? – Vivek V K – 2017-02-23T00:36:16.327

This may as well be untested too, because it doesn't work. – hmedia1 – 2019-10-13T10:25:09.847

@VivekVK It's possible. The proof of concept is that it can be done from the accessibility keyboard (without any actual keyboard connected), so the APIs are there ... somewhere – hmedia1 – 2019-10-13T10:26:31.470

0

I have this script on my "display" iMac and just screen share from the portable to click and run it.  If the iMac is locked I use Remote Desktop to unlock it.  I'm sure there is a more efficient way, but it is super easy...

tell application "System Events"
    key code 144 using {command down}
end tell

David

Posted 2013-01-23T23:58:44.820

Reputation: 1