Back Original

Magic Switch: Share Apple Magic keyboard/track-pad/mouse between two Macs

TL;DR: Magic Switch is a free, open-source macOS app for sharing an Apple Magic Keyboard, Trackpad, or Mouse between two Macs — with no Apple ID and no USB re-pairing dance. Source on GitHub.

I recently finally returned to Poland, after ~living in Taiwan for 7 months, where I was … bike riding, learning Chinese, and getting some experience in certain things that I never thought I’d do in my life. Upon my return, I decided that it’s finally time to admit that I’m no longer just a tourist, and make my (rental) apartment look kind of nice. I like interior design a lot, but for the past 5 years of living in my current place, couldn’t – despite the overall low cost – bring myself to invest in anything but the essentials that I would regularly use or need. I could live out of a backpack: I have done that, for months at a time, of course. I came to Poland with half a suitcase. I need nothing but the essentials to live. Most of my belongings aren’t valuable. But the sentimental value makes them priceless. It’s not that my place wasn’t nice; in fact, of the many people I’ve hosted on Couchsurfing, they’ve all commented that they liked the way I designed, orientated, and chose things for the apartment. They were mostly essentials.

So I got back to Poland, and have been doing a whole revamp of my apartment. Taiwan inspired me a lot; I was moving around every week or two, and the places I stayed, the places I explored, and the galleries, studios, and museums I visited all took their toll. I’m a minimalist at heart, clearly, but enjoyment of life and the conveniences have finally gotten the best of me: maybe I’m just getting old. If I get kicked out of here any time soon, I’ll be devastated: because my hard work of picking colors that match, materials that suit, and difficult-to-source designs and items, probably simply won’t fit the place I live in next; the tiles on the kitchen wall will be different, the furniture will be a different palette, or the curtains will be a different color. Annoying.

I decided to buy a real desk for the first time in my life. Normally, I work either in bed, or laying down somewhere else. Sitting at a desk is generally a painful experience for me and has been forever – sitting in general is extremely painful; after ~5 minutes, my back starts to spasm like no other pain I’ve ever (regularly) felt.

I probably have Ehlers Danlos Syndrome which, among other things (like my extreme flexibility), has probably led to back pain problems which no doctor, surgeon, physio, or “friends offering help”, could help with. The only time I’ve encountered what the pain feels like, was in Orwell’s 1984:

[..] his body was being wrenched out of shape, the joints were being slowly torn apart [..]

So, I got myself a standing desk!

The new standing desk: monitor, Magic Keyboard, and Magic Trackpad up top, bike parked underneath

Yes, today’s blog is a bit of a mix of storytelling, and technical details. Bear with me, we all have our demons.


Among other things, I also got a monitor, which meant it was time to get a keyboard too. I opted for the option that was free: an Apple Magic Keyboard and Magic Trackpad. These two are Bluetooth devices, which require an initial pairing with USB-C.

Apparently Apple, in all of their “it just works” wisdom, made it so these peripherals couldn’t be connected to multiple devices at once. Unlike Apple’s AirPods, which you can connect to multiple devices at once and easily just connect from any device whenever you want to use them on a device, the Magic Keyboard and Magic Trackpad simply refuse to be paired with multiple devices at once, and using them with more than one computer is extremely painful. Actually, it’s worse than that: you can’t just disconnect the peripherals on the device using them and connect to them on the other device; no, you have to connect them via USB to the other device to do a full re-pair. Basically, the peripherals are really sticky.

You can have two devices that are paired to the single peripheral, but good luck getting the peripheral to connect to the second device: you can shut off the first device or disconnect Bluetooth; it just Won’t Work™. This is a known issue. In reality, there is a real way to share the peripherals between devices, but it requires being signed into the same Apple Account on both of the devices. You go to Settings -> Display -> Advanced -> Link to Mac or iPad, and it can work that way. That doesn’t fit my use-case, because I don’t want to (and can’t, in some cases) login to my Apple Account.

blue-switch

There’s some paid product you can buy on the Apple Store which claims to help with this. I found some open-source software called blue-switch, which made some very simple menu bar UI, which could be used to disconnect a peripheral from one system, and get it connected to another. Basically, the way it worked was that:

Basically, it was a hack around the design of the Magic devices, and you basically just force the re-pair dance so it happens at a click, instead of using the USB cable to re-pair and dealing with the Bluetooth settings.

The problem with blue-switch was that it was just a very simple proof of concept, trusted everybody on the LAN, and the UX was basically non-existent (and didn’t even have the ability to handle errors). As Claude tells me, this is what the original app did:

Without Claude, I’d have no way of knowing how this worked (other than reading the code and hoping I understand the macOS Bluetooth glue). People treat these LLM coding agents as slaves, but clearly, I’m a slave to the LLM.

So anyways, that’s all blue-switch was. It didn’t have authentication, encryption, any idea of which device the other Mac actually was, and no recovery on failure (which seems to be completely unavoidable sometimes). Beholden to my master, Claude and I vibe-coded a more secure, resilient application, with a proper UI/UX and algorithm to never “lose track” of these devices.

Magic Switch: a free alternative

I called it Magic Switch.

Magic Switch app icon

The source code is available on GitHub, as well as an immutable build, built with GitHub runners.

Security

For security, I went with:

One may question commitment to sparkle motion why I bothered with all of that for a simple LAN-only connection which just tells the other client “connect to this Bluetooth device; disconnect from that Bluetooth device”. In reality, it’s because I wanted to learn things. And have fun.

Smoothing out the handoff

As for actual functionality change, I added a bunch of new things to make the app and handoff smoother, and Just Work™.

Pairing

Pairing two Macs works a lot like pairing most Apple things: one generates a code, the other types it in, and then both show a matching fingerprint you can eyeball.

Pairing tab before setup
The Pairing tab before setup: generate a code, or enter the one your other Mac made
Generating a pairing code Entering the pairing code on the other Mac
Generate a 12-character code on one Mac… …and enter it on the other
Paired, showing the matching fingerprint
Once paired, both Macs show a matching fingerprint — a mismatch means something’s wrong

Sleep, clamshell, and other annoyances

The real problems arise when we try and get things working when it comes to sleep, clamshell mode (lid closed but connected to a monitor), and some other annoying network things. I came up with some useful configurable functionality:

Who actually owns a peripheral?

Under all this handoff stuff, there’s basically just one question: “who actually owns a peripheral right now?” This isn’t “stored” in the application anywhere, and instead we literally just take “owning a peripheral” to mean currently having a live Bluetooth connection to it, and the app works that out every time, with either:

From there, who gets the peripheral comes down to a few cases:

Generally speaking, there’s some more logic, especially about:

The UI

For the UI, I made a much more polished display and also menu-bar popdown.

The Magic Switch menu-bar popdown
The menu-bar popdown: click a Mac to move everything, or a single peripheral to move just that one; a checkmark means it’s on this Mac

Behind the menu sits a small settings window with a tab for each job:

Peripheral settings tab Device settings tab
Peripheral tab: register a Magic device, hand it back, or stop managing it Device tab: ping the other Mac, share peripherals to it, or forget it
Other settings tab
Other tab: launch at login, license info, and the manual update check

The popdown menu in the menu bar was the hardest thing to get working as I wanted it to. I had the following requirements:

I imagine nobody else will ever actually use this: I certainly wouldn’t recommend it! Nonetheless, it was kind of fun to make.