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:
- Sinergy / Teleport: Apps to share your mouse and keyboard between different computers, but they don’t work after you activate target display mode.
- 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.
- 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.
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.
# Disable BT on remote machine and enable it on local machine, so KB/Mouse reconnect to local machine.
#ensure local bluetooth is off
#disable imac bluetooth
ssh firstname.lastname@example.org '/usr/local/bin/blueutil off'
#enable local bluetooth
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)
#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 email@example.com:~/
#log on to the remote computer
#create the .ssh directory
#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
#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.
# Disable BT on local machine and enable it on remote machine, so KB/Mouse reconnect to remote machine.
#ensure local bluetooth is off
#enable imac bluetooth
ssh firstname.lastname@example.org '/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.
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!