Building a Keyboard and Mouse Switcher in Rust

Back when Linux was my primary operating system, I built a small Rust program to switch my keyboard and mouse between operating systems.

It was not an ambitious project. I had no plans to distribute it or turn it into a feature-rich application. I simply wanted to solve an annoyance in the way I used my own computer.

Looking back, even this small tool involved quite a bit of trial and error: interacting directly with input devices, dealing with USB switching, and, unfortunately, ending up without any surviving source code.

In this post, I want to explain why I built it, how it worked, and what the experience taught me.

1. Running two operating systems at the same time

First, a brief explanation of my setup at the time.

I used Linux as my main operating system and ran Windows in a virtual machine. Unlike conventional dual booting, this allowed both systems to run simultaneously.

Using GPU passthrough, I assigned a separate graphics card to the Windows VM and set things up so I could use Linux and Windows on their respective monitors.

I have already covered how I built that environment and why I can no longer use it in “Why I Left Linux and Went Back to Windows”. Here, I will focus on a particular problem that came with it: input devices.

Being able to run both operating systems at once was great. But once I started using the setup regularly, one inconvenience became surprisingly noticeable.

The keyboard and mouse.

2. How should I switch the keyboard and mouse?

Running Linux and Windows simultaneously does not automatically solve the question of which operating system receives input.

One common approach is to connect a separate keyboard and mouse to each system. Another is to use a KVM switch to share input devices.

I wanted to try something different.

My main concerns were desk space and convenience.

Having two keyboards does not necessarily take up an unreasonable amount of room. The real problem is where you have to place them.

Normally, you keep a keyboard within comfortable reach. With two on the same desk, one tends to sit in front of you while the other is farther away.

If both are regular typing keyboards, switching operating systems means moving a keyboard or reaching for the other one every time.

That bothered me.

I frequently moved from Linux to Windows and back again. Being able to change operating systems instantly did not feel very useful if I also had to rearrange my keyboard each time.

So I came up with a way to use two keyboards and one mouse while giving the two keyboards completely different roles.

The keyboard directly in front of me would remain my main typing keyboard. The second keyboard, placed farther up the desk, would act as a control device for switching operating systems.

I also wanted to share one mouse rather than keep a separate mouse for each environment.

3. Turning a keyboard into a control device

There was one important problem with this idea.

Once I passed my main keyboard and mouse through to the Windows VM, Linux could no longer receive input from those devices.

To return from Windows to Linux, I needed a way to hand them back to the Linux host.

But if the devices capable of issuing the switching command were already connected to Windows, how could I trigger the command on Linux?

My solution was to leave a second keyboard permanently connected to Linux.

I did not use that keyboard for ordinary typing. It was essentially a physical control panel.

I wrote a Rust program to watch its input.

The design was fairly simple.

Once Linux recognized the keyboard as an HID (Human Interface Device), the Rust program read raw input events from that device.

Normally, keyboard input is processed by the operating system before being delivered to applications. In my case, I did not want a designated keypress to behave as text input or a normal shortcut. I wanted it to trigger a system-control action.

So the program monitored events from the specific keyboard and performed a predefined action when a particular key was pressed.

The key I chose was Esc.

Pressing Esc on the control keyboard caused the Rust program to receive the event and start the switching procedure. I also arranged for that keystroke not to reach the operating system as ordinary input.

This meant that even when the main keyboard and mouse were attached to Windows, I could still trigger their return using the control keyboard connected to Linux.

I did have a second keyboard on the desk, but I no longer needed to move the keyboard I actually typed on.

That was exactly the experience I wanted.

4. Why I delegated switching to a shell script

The Rust program's primary responsibility was detecting input.

When it recognized a designated keypress, it launched the corresponding Linux shell script.

Why did I not implement the entire USB switching procedure in Rust as well?

Mostly because of the problems I encountered during development.

Switching input devices was less straightforward than I initially expected.

Sometimes the system failed to find the intended device ID or USB identifier. At other times, a device passed to the other operating system would not be recognized immediately.

Most of my difficulties occurred while detaching and reconnecting devices, not while reading keyboard input.

Receiving a raw event and determining which key had been pressed was relatively uncomplicated.

The actual switching procedure, on the other hand, needed adjustments depending on the system configuration and the state of the devices.

That led me to separate the responsibilities.

Rust detected the input; a shell script performed the actual device switching.

If something went wrong in the switching sequence, I could change the script without modifying and recompiling the Rust program.

I could simply edit the shell script and try again.

For example, if I needed to change how a device was identified or adjust a step in the switching process, I only had to update the script.

The program did not need many features at the time. My priority was making one specific switching action work reliably, not adding a wide range of functionality.

I still think that separation made sense for the problem I had.

Keeping the frequently changing part outside the compiled program made it easier to maintain.

5. Why did I choose Rust?

For software that interacts directly with operating-system input devices, C or C++ would also have been reasonable choices.

Both languages have long been used for systems programming and software that works close to operating systems and hardware.

I chose Rust instead.

The simplest reason is that I was particularly interested in Rust at the time, and it was my favorite programming language.

But personal preference was not the only reason.

This was somewhat different from a typical utility that runs a task and exits. It needed to stay active for as long as I was using the computer.

Launching the program only when I wanted to switch keyboards would not have delivered the experience I was after.

It needed to keep running in the background, waiting for keyboard input.

For a long-running process like that, I also wanted to consider stability over time.

Rust's ownership and borrowing rules help catch many memory-safety problems during compilation. I also appreciated that it does not rely on a conventional tracing garbage collector.

Of course, writing a program in Rust does not guarantee that it will always work reliably. Logic errors are still possible, as are problems involving the operating system or external devices.

Even so, Rust was a good fit for the kind of systems programming I wanted to do.

I discussed my broader reasons for liking the language in “Why Rust Is My Favorite Programming Language”.

6. The source code disappeared

This would be a good place to show the actual source code or an implementation example. Unfortunately, I cannot.

I lost the source code.

It is a little embarrassing to admit.

At the time, I had no intention of publishing or distributing this program.

It was a tool made solely for my own convenience, so I never uploaded it to a remote repository such as GitHub.

The program worked, and I continued using it.

The trouble came later, when I replaced my computer and cleaned up my old environment.

I assumed I must have backed up the source code somewhere.

I regularly managed other projects and files, so I vaguely believed this one would have been included in a backup too.

But after formatting the system, I discovered that the source code was nowhere to be found.

Assuming something has been backed up and actually having a backup are two very different things.

In the end, I lost the entire source code.

I still remember the general design and how the program behaved, but not the precise implementation.

That is why I am not going to invent example code and present it as something I originally wrote.

Describing the design and behavior as accurately as I remember them is the most honest record I can provide now.

7. Would I still need the same program today?

I no longer use the same computing environment.

After upgrading my computer, I found it difficult to recreate the GPU passthrough setup I had used before.

In particular, problems with IOMMU grouping made it difficult to isolate devices in the way I wanted. I eventually returned to using Windows as my primary desktop operating system.

I explained that story in my earlier Linux and Windows post, so I will not go into the details again here.

What if I had been able to rebuild the same virtualization environment on my new computer?

I would probably have needed that input-device switching program again.

But without the source code, I would have had to develop it from scratch.

Having built it once, I might have completed it faster the second time. I remember the overall architecture and the issues I encountered.

Still, it would have been frustrating to solve a problem I had already solved.

If I had kept the program, I could have tried adapting the existing code to the new environment instead.

8. Even personal programs need backups

The part I regret most about this project is how poorly I managed its source code.

At the time, I did not expect anyone else to see it, let alone use it.

So I saw little reason to create a repository or release it.

Looking back, though, publishing software and backing up its source code are completely different matters.

A program can be valuable to its author even if it is never made public.

Code written to solve a problem can also become useful reference material years later.

Back then, all that mattered was getting the feature to work. Now I would like to look back at the implementation and understand exactly what I did.

This project left me with two important experiences.

First, I learned that I could shape my computing environment by working directly with operating-system input devices.

Second, I learned that even a small, entirely personal program deserves a proper source-code backup.

Closing thoughts

The Rust program I have described was not a sophisticated piece of software.

I built it because switching my keyboard and mouse between Linux and Windows was inconvenient, and I wanted to make my own setup easier to use.

Along the way, I gained experience with operating-system input events, USB device switching, and combining Rust with Linux shell scripts.

It was also a project that reminded me why I enjoy Linux.

On Linux, I can inspect many aspects of how the system works and, when necessary, change my workflow with scripts or custom programs.

Not every operation is easy or unrestricted, of course. But having the freedom to shape a system around my needs is one of the main reasons I prefer Linux.

Above all, this project captures what I enjoy about programming.

I did not build it to impress anyone. I built it because I wanted to make the computer I use a little more convenient for myself.

The code is gone, but I can still record the problem I faced and how I approached solving it.

From now on, I intend to keep the source code for even these small personal projects.