Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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!

share|improve this question
up vote 3 down vote accepted

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.

share|improve this answer
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 Jan 24 '13 at 0:27
I was able to do it with I wonder what it sends as command to make it work. – iDev247 Jan 24 '13 at 0:51
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 Nov 9 '13 at 15:04

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/, on the the target display machine.

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

ssh username@target-display-machine "~/bin/"

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.

share|improve this answer
This 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 Jul 17 '14 at 9:26

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
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .