13

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!

3 Answers 3

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.

3
  • 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
    Jan 24, 2013 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, 2013 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. Nov 9, 2013 at 15:04
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.

5
  • 1
    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. Jul 17, 2014 at 9:26
  • 1
    This 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
    Feb 23, 2017 at 0:36
  • This may as well be untested too, because it doesn't work.
    – hmedia1
    Oct 13, 2019 at 10:25
  • @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
    Oct 13, 2019 at 10:26
  • not working for me
    – wukong
    Mar 18, 2020 at 11:43
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

You must log in to answer this question.

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