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

keyboardlinuxmacosssh

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!

Best Answer

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.

Related Question