Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Is it possible to send a keystroke via SSH? I want to be able to send CMD+F2 via ssh from/to Mac OSX. If not possible a mouse solution would also be good.

The reason I need this is because I only have one keyboard (but two mouse). 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 be able to use my mouse mouse on the original iMac would save me loads of trouble!

share|improve this question

2 Answers 2

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 keyboardmaestro.com/main... 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/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.

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 at 9:26

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.