How to share your bluetooth keyboard on target display mode.

A quick way to share your bluetooth keyboard between your iMac and Macbook.


If you have an iMac, a Macbook and you like to use the iMac on target display mode. Just like me. You probably had a tough time trying to share your bluetooth keyboard with you macbook, but not anymore. After a long time researching about an easy way to share the iMac’s keyboard with my Macbook, I finally got a solution.

First, I will talk about the common solutions found on the internet:

  1. Sinergy / Teleport: Apps to share your mouse and keyboard between different computers, but they don’t work after you activate target display mode.
  2. 1Keyboard: An app to type on other devices without having to pair with the keyboard. You can easily switch to iPhone and type there, or switch to iPad and type there too. But sadly it didn't work well with the Macbook, specially when in target display mode.
  3. Share screen: The method I was using till now. You have to activate target display mode, then go to you macbook and login to your iMac via Share Sreen to switch the bluetooth off. Then switch your macbook’s bluetooth on. So, the keyboard should now pair with your macbook.

As you can see, the share screen method was a pain in the ass. Everytime you wanted to use target display mode you had to go through the same boring steps. I was not conformed with that. It had to be a better way.

That’s when I found Hugh Saunders' article presenting a clever solution using blueutil and ssh to steal and release your iMac’s keyboard. You can check his article here.

The first chunk of code is meant to “steal” the keyboard from your iMac. First, it makes sure your macbook’s bluetooth is set off. Then it connects via SSH to your iMac and switches off the bluetooth. Finally it switches on your macbook’s bluetooth "forcing" the keyboard to pair with it.

#!/usr/bin/env bash
# Disable BT on remote machine and enable it on local machine, so KB/Mouse reconnect to local machine.
#ensure local bluetooth is off
blueutil off
#disable imac bluetooth
ssh user@imac.local '/usr/local/bin/blueutil off'
#enable local bluetooth
blueutil on

To enable the SSH to run smoothly (and without prompting for password) between your machines, you will have to do a one time ssh key configuration. You can go through the steps below on your terminal to activeted a "passwordless" ssh (full article with the steps).

# generate pub and priv keys, leave the passphrase empty
# (simply press ENTER when asked for it)
ssh-keygen

#copy the pub key to the remote computer
#change "user" to your user name
#change "imac" to your domain name
scp ~/.ssh/id_rsa.pub user@imac.local:~/

#log on to the remote computer
ssh user@imac.local

#create the .ssh directory
mkdir .ssh

#append key to file
cat id_rsa.pub >> ~/.ssh/authorized_keys

#delete the public key file, no longer needed
rm -f id_rsa.pub

#log off the remote server
exit

#logon to the remote server, without password prompt
ssh -2 -p 22 user@host

The second chunk of code is meant to "release" the keyboard back to your iMac. First, it switches off your macbook’s bluetooth. Then, it connects via SSH to your iMac and turn the bluetooth on. This way the keyboard will pair back with the iMac.

#!/usr/bin/env bash
# Disable BT on local machine and enable it on remote machine, so KB/Mouse reconnect to remote machine.
#ensure local bluetooth is off
blueutil off
#enable imac bluetooth
ssh user@imac.local '/usr/local/bin/blueutil on'

Fabulous solution. But then again, I would have to run one file to steal the keyboard and when done I would have to run the other file to release the keyboard. Boring steps!

So, I realized I could make this process a lot easier with a workflow/service created in Automator and binding it to a system-wide shortcut. That's exactly what I did.

The first block is supposed to run blueutil status on you local machine and the result will be passed to the next block as a parameter. The second block will test if your bluetooth is on, off or it will debug. If it is off, it will run the script to steal the keyboard from your iMac. If it is on, it will run the script to release the keyboard to your iMac.

Apple script code on automator to automate the process of toggling bluetooth on and off.

Finally, you have to go to the system preference panel choose keyboard and go to shortcuts. Check the screenshot below, I have activated the btToggle service and binded it to a shortcut, in this case [CTRL][SHIFT][CMD][B].

And it’s done!

Show your support

Clapping shows how much you appreciated Guto Foletto’s story.