Magic Switch:在两台 Mac 之间共享 Apple Magic 键盘、触控板或鼠标
Magic Switch: Share Apple Magic keyboard/track-pad/mouse between two Macs

原始链接: https://joshua.hu/magic-switch-easily-switch-magic-keyboard-trackpad-mouse-between-mac-macbook-macos

在台湾生活了七个月后,作者回到波兰,开始布置一套极简公寓。在旅行反思与慢性背痛的双重背景下,他逐渐添置了显示器和升降办公桌。 最终诞生的项目 Magic Switch 是一款免费、开源的 macOS 应用,可以让两台 Mac 共享 Apple Magic Keyboard、Magic Trackpad 或 Magic Mouse,无需使用同一个 Apple Account,也无需通过 USB 重新配对。它改进了概念验证项目 blue-switch,支持单台或全部设备切换、设备归属检查、切换失败时回滚、睡眠及 clamshell 模式恢复、更新检查,并提供了更精致易用的菜单栏和设置界面。 通信安全机制包括共享配对码、通过 PBKDF2 派生并保存在钥匙串中的凭据、HKDF 会话密钥、HMAC 通信记录验证、ChaCha20-Poly1305 加密、重放防护、速率限制、身份指纹检查、超时设置和 IP 地址规范化。系统会根据实时蓝牙连接以及两台 Mac 之间的协调来判断设备归属,从而尽量减少竞态问题,并避免外设滞留在某台 Mac 上。 Magic Switch 使用 Claude 构建,是一个学习项目,目前已在 GitHub 上提供,并配有不可变构建版本。

Magic Switch 可在两 台 Mac 之间共享 Apple Magic Keyboard、Magic Trackpad 或鼠标。据报道,切换过程既不需要 Apple ID,也不需要重复进行蓝牙或 USB-C 配对。 “黑客新闻”的讨论指出,苹果原生支持的“通用控制”已经能够处理许多 Mac 之间的操作,尤其是每台 Mac 分别登录不同 Apple ID 的情况。对于跨平台使用,评论者推荐了长期使用的 Synergy,以及它的开源替代方案 Deskflow 和 Barrier,其中多个项目都提供预编译版本。 不同工具的缺点各不相同。Synergy 可能与 KVM 式显示器切换流程发生冲突;软件解决方案则可能难以稳定处理 Magic Trackpad 的配对、休眠后的重新连接、剪贴板可靠性,以及键盘布局映射,因为某些字符是按字符而非按键码传输的。 总体而言,Magic Switch 可能更适合希望轻松切换外设、且只使用 Apple 设备的用户;而对于设备平台不统一的系统,Synergy 或 Deskflow 仍然更灵活。
相关文章

原文

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.


Why you can’t share a Magic Keyboard between two Macs

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

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:

  • Using mDNS, two devices negotiate a connection with each other.
  • Both devices can send/receive various commands, for example sending to the other device something along the lines of: “DISCONNECT FROM PERIPHERAL AA:BB:CC..”
  • The device will then force a connection to the peripheral.

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:

  • Discovery via mDNS/Bonjour — two Macs running the app find each other on the local network and exchange routing info (host + port).
  • A plaintext TCP control channel — once discovered, either Mac opens an NWConnection to the other and sends short string commands: CONNECT_ALL, UNREGISTER_ALL, NOTIFICATION, SYNC_PERIPHERALS, HEALTH_CHECK, plus OP_SUCCESS / OP_FAILED acks.
  • The core Bluetooth trick — on receiving CONNECT_ALL, a Mac uses IOBluetooth to force-pair/connect the Magic peripherals to itself; on UNREGISTER_ALL it drops them.
  • A basic menu-bar UI — a status item with a list of registered peripherals and the other Mac, and a Settings window with a few tabs.

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

Magic Switch app icon

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

Security