Building a USB KM Switch: Hardware Solutions for Network-Free Keyboard Sharing

November 4, 2025

Connecting two computers directly via USB to share keyboard input presents a unique challenge, primarily due to the asymmetric nature of USB: one side is always the "host" (like a computer) and the other a "device" (like a keyboard or mouse). Most computers are built to be USB hosts, making a direct computer-to-computer link non-trivial, especially when aiming for a solution that avoids traditional network protocols. The goal is often to use a laptop's keyboard to control a headless machine, effectively creating a custom Keyboard-Mouse (KM) switch.

Hardware Solutions: The Microcontroller Path

A common and flexible approach involves using microcontrollers to bridge the gap. These small, specialized computers can be programmed to behave as USB devices, such as keyboards.

  • Two Microcontrollers: A robust solution involves using two microcontrollers, one connected to each computer.
    • The first microcontroller (connected to the source laptop) acts as a USB host, receiving input from the laptop (e.g., via a custom driver or simply reading its own GPIOs if keys were attached). It then sends this data to the second microcontroller.
    • The second microcontroller (connected to the target headless computer) acts as a USB HID (Human Interface Device) keyboard. It receives data from the first microcontroller and translates it into keystrokes, sending them to the target machine.
    • Recommended Boards: Popular choices for this include Raspberry Pi Picos, RP2040-based boards (like Waveshare RP2040-Zero), ESP32-S3 boards (which often feature dual USB-C ports simplifying the design), Teensy boards, or even multiple Arduinos. Some microcontrollers, like certain STM32 series (e.g., STM32F407/417), have dual USB ports that can both operate as devices.
    • Inter-Microcontroller Communication: The two microcontrollers can communicate using simple protocols like UART (serial) or SPI, which are standard on most boards. This setup requires custom firmware development for both microcontrollers.
    • Example Projects: Open-source projects exist for converting Raspberry Pi Zeros into HID devices (zero-hid) or for creating composite joysticks, which can be adapted for keyboard emulation.

Simpler Hardware: USB-to-Serial Adapters

For a less complex hardware setup, two USB-to-serial adapters combined with a null modem cable can create a direct communication link.

  • How it Works: Each computer connects to a USB-to-serial dongle. A null modem cable then directly links the serial ports of the two dongles (TX to RX, RX to TX, and shared ground).
  • Software Requirement: While the hardware is simpler, software is still needed on both computers to read/write serial data. This software would translate keyboard events on the source and inject them as keystrokes on the target. This typically involves using a serial terminal or a custom application.
  • Off-the-Shelf Options: Specialized USB-to-serial null modem cables are available that integrate this functionality into a single cable.
  • Considerations:
    • Ground Isolation: When connecting two different computers, their electrical grounds might not be identical, potentially leading to ground loops or even damage. Using optical isolators between the serial lines is a recommended safety measure.
    • Security: Any device acting as a keyboard can be a security risk. If the target machine is compromised, it could theoretically send malicious keystrokes back to the source machine if the link is bidirectional, or at least control what happens on the target. Operating systems like macOS often require user confirmation when a new "keyboard" is connected, offering a partial security barrier.

Advanced Software Approach: USB Gadget Mode

In some specific cases, a purely software-driven solution on the source laptop might be possible if its USB controller supports "gadget mode" (also known as "device mode").

  • Concept: Instead of acting as a typical USB host, the laptop's USB port is configured to behave as a USB device (e.g., a keyboard).
  • Feasibility: This capability is not universal and often requires specific hardware support (e.g., certain Intel XDCI controllers) and an operating system (like Linux) that can leverage the "USB gadget framework."
  • Benefit: If viable, this eliminates the need for any external hardware, offering the most integrated solution.

Addressing Common Pitfalls

  • USB Asymmetry: The core challenge is USB's host-device architecture. Solutions must account for one device acting as a "device" (keyboard) for the target computer.
  • Network Avoidance: While network-based KVMs are popular, if strict network isolation is required, the hardware solutions outlined above are more appropriate. Even using USB-to-Ethernet adapters is a form of networking, albeit a direct, isolated one.
  • Overkill Concern: Using a Raspberry Pi Zero (which runs a full OS) for simple HID emulation might seem like overkill for some. However, modern microcontrollers like the RP2040 or ESP32-S3 offer excellent capabilities for this without a full OS, combining microcontroller simplicity with powerful USB features.
  • Security: Always consider the security implications of a device that can inject arbitrary keystrokes. It's akin to physically typing, meaning a compromised device could execute commands on the target system.

Conclusion

While directly linking two computers via USB for keyboard control isn't as straightforward as plugging in a standard keyboard, several viable paths exist. Microcontroller-based solutions offer the most flexibility and control, allowing for custom behavior and robust HID emulation. Simpler USB-to-serial links provide a quick, hardware-minimal option but require careful consideration of electrical isolation and security. The "best" approach depends on the desired level of hardware involvement, programming comfort, and security/reliability requirements.

Get the most insightful discussions and trending stories delivered to your inbox, every Wednesday.