I'm excited to announce the first version of Lokey! Lokey is an open-source Rust framework for building input device firmware that I started working on ~2 years ago.
The core of the framework as well as some of its more advanced features are now in a state that I'm pretty happy with, so I decided to create the first release (version 0.1.0).
What is Lokey?
But let's start at the beginning: what exactly is Lokey and what can you use it for?
You can think of Lokey as a collection of building blocks that you can mix and match as you like to build firmware for input devices. By "input device" I mean any device that sends data to a host (like a PC or smartphone), such as:
- Keyboards
- Mice
- Touchpads
- MIDI controllers
- Gamepads
- ... and more
In the case of a keyboard, the firmware you build with Lokey handles key scanning (detecting which key was pressed), mapping pressed keys to actions (e.g. key codes that can be sent to the host), and actually sending the data to the host via a communication channel like USB or Bluetooth.
Lokey provides a way for you to organize these features into components and transports (more on this later), which makes your firmware very modular and easy to build and maintain. It also includes common implementations, like for the keyboard features mentioned above (see What Version 0.1.0 Includes for details).
Why I Built It
I originally started Lokey as a firmware purely for keyboards. There are already great keyboard firmware projects out there (like QMK, ZMK, RMK, and others), but when I started designing my own keyboard, I noticed that I had to design the hardware around what those firmwares supported if I didn't want to maintain a fork. While it is possible to extend the more established keyboard firmwares to some extent, in my experience it's often difficult, and some things just aren't possible.
So my main goal with Lokey was to make it extensible. I wanted an easy-to-use, well-documented API that lets you write whatever custom functionality you need, so you only have to maintain your own code instead of a fork of the whole firmware.
Note
If you're already happy with an existing keyboard firmware, there is no reason to switch. Lokey is still missing some of the more advanced features that those projects offer, though reaching feature parity is planned for the future (see Comparison with Alternatives for a more detailed overview).
Pretty quickly into developing Lokey, it became apparent that a lot of the concepts also apply to other input devices and with the modularity I had built, it would be easy to factor the keyboard functionality out into its own crate and keep the Lokey core completely independent of keyboards. That's how Lokey ended up becoming a general framework for input devices rather than just keyboards.
And even though Lokey started out as a keyboard-only firmware, I actually think it might be even more useful for other kinds of input devices, since there are generally fewer alternative firmware projects for them. I see Lokey as an especially good fit for MIDI controllers, as they are much less standardized than keyboards and often require custom functionality.
Quick Overview
Let's take a look at how the Lokey framework can be used.
Lokey introduces the following concepts:
- Components: Firmware behaviour that runs on the input device.
- External Transports: The communication channel to the host (e.g. USB, Bluetooth).
- Internal Transports: The communication channel between multiple device parts. This is required if your input device consists of multiple separate device parts (such as a split keyboard) that need to communicate with each other.
- Devices: Devices declare the microcontroller the input device runs on, the external and internal transports it uses, and what components are supported.
This provides a nice abstraction that makes it easy to write firmware.
Here is a simple example:
#![no_main]
#![no_std]
use embassy_executor::Spawner;
use lokey::Context;
use lokey_device_foo::{DefaultState, Foo, NUM_KEYS};
use lokey_keyboard::{Key, MatrixConfig, Scanner, layout};
#[lokey::device]
async fn main(context: Context<Foo, DefaultState>, spawner: Spawner) {
let scanner = Scanner::<MatrixConfig, NUM_KEYS>::new();
let layout = layout!([Key::A, Key::B, Key::C, Key::D]);
context.enable_all((scanner, layout)).await;
}In this example, Foo is a type that implements lokey::Device, and scanner and layout are components that are run with context.enable_all. The scanner component takes care of scanning what keys were pressed/released and the layout component maps the pressed keys to actions (in this case sending the key codes A/B/C/D to the host).
The implementation of Foo would, for example, look like this:
use lokey::external::NoMessage;
use lokey::storage::DefaultStorageConfig;
use lokey::{AnyState, ComponentSupport, Context, Device};
use lokey_keyboard::{ActionContainer, KeyboardReport, Layout, Matrix, MatrixConfig, Scanner};
use lokey_nrf::Nrf;
pub struct Foo;
impl Device for Foo {
// Use the nRF microcontroller (exact microcontroller is selected via features).
type Mcu = Nrf;
// Use the default persistent storage config.
type StorageConfig = DefaultStorageConfig;
// Use Bluetooth Low Energy to communicate with the host.
//
// KeyboardReport is used as the outgoing message type for the key codes and NoMessage is
// used as the incoming message type as we don't need to receive anything from the host.
type ExternalTransport = lokey_ble::external::Transport<Nrf, KeyboardReport, NoMessage>;
// Devices without multiple parts can use the empty internal transport.
type InternalTransport = lokey::internal::empty::Transport<Nrf>;
// (Configuration omitted here for brevity)
}
pub const NUM_KEYS: usize = 4;
pub type NumKeys = <typenum::Const<NUM_KEYS> as typenum::ToUInt>::Output;
// Add support for lokey_keyboard::Layout to the device.
impl<S: AnyState, A: ActionContainer<NumChildren = NumKeys>> ComponentSupport<Layout<A>, S>
for Foo
{
async fn enable(component: Layout<A>, context: Context<Self, S>) {
component.run(context).await;
}
}
// Add support for lokey_keyboard::Scanner<MatrixConfig, 4> to the device.
impl<S: AnyState> ComponentSupport<Scanner<MatrixConfig, NUM_KEYS>, S> for Foo {
async fn enable(component: Scanner<MatrixConfig, NUM_KEYS>, context: Context<Self, S>) {
// Use the matrix scanner and specify which pins the keys are connected to.
let matrix = Matrix::new::<NUM_KEYS>(/* input pins */, /* output pins */);
// Map the physical key switch pins to logical key indices.
let matrix = matrix.map_rows_and_cols([0, 1], [0, 1], 0);
component.run(matrix, context.as_dyn()).await;
}
}Note
Device definitions like this are intended to be provided as separate crates. If someone else has already written one for your device, you can just depend on that crate, and the first code snippet is actually all you need to get the working firmware.
Lokey also provides abstractions for other things, like persistent storage and shared state, but I won't go into detail here as it's out of scope for this post. You can check out the documentation to learn more.
Design Philosophy
When developing Lokey, I particularly paid attention to the following 3 aspects:
- API user-friendliness
- Compile-time checks over runtime checks
- Performance
I went through multiple iterations of the API to find a good balance between those aspects, and I think I landed on a very good result at this point.
There were certain situations where I made a tradeoff to e.g. use a little bit more RAM in order to keep the API cleaner and simpler to use, so for very constrained microcontrollers Lokey might not be the best choice. However, for anything but the most constrained setups, Lokey is a great fit.
Dynamic Configuration
Lokey can also be configured at runtime (for example, remapping keys without reflashing the firmware) using an extensible protocol called Odyn.
Enabling it on a device only requires adding the OdynCore component and swapping out the static components for their Odyn variants (for example, OdynLayout instead of Layout).
Note
Compared to the rest of the framework, Odyn is still in an earlier stage of development, so expect breaking changes in the future.
Lokey Editor is a web app that uses the Odyn protocol to provide a graphical interface for configuring Lokey devices. Rather than being tied to a single device, it queries the connected device for its ID and capabilities and adapts its UI to match what that device supports.
Just like the firmware framework itself, Lokey Editor is designed to be extensible. Its core functionality and components are split into separate packages so you can build your own version of the editor if needed. This is useful if you want to support custom device features that don't make sense to upstream, or if you're building a commercial device and want to have your own branding in the editor.
You can try it out yourself at https://editor.lokey.rs.


Screenshot of Lokey Editor
What Version 0.1.0 Includes
This version of Lokey includes support for the following functionality:
- Core abstractions for devices, components, transports, persistent storage, state, and more
- USB communication to the host
- Bluetooth Low Energy communication to the host
- Bluetooth Low Energy communication between device parts
- nRF52840 microcontroller support
- RP2040 and RP235x microcontroller support
- Keyboard functionality
- All functionality needed for basic keyboards is included (e.g. matrix scanning, direct pin scanning, layers, etc.), as well as some more advanced features (e.g. macros, hold-tap behaviour, key overrides, etc.)
- Basic mouse functionality
- Mouse movement and mouse button events can be sent to the host via USB and Bluetooth Low Energy
- Built-in keyboard actions that e.g. move the mouse pointer or press mouse buttons
- Basic MIDI controller functionality
- MIDI 1.0 messages can be sent to the host via USB and Bluetooth Low Energy
- Initial support for dynamically configuring devices
The core API of the framework has already undergone multiple overhauls and refinements by now. I think it is at a good point to start building on it without expecting too many changes in the future.
Note, however, that I don't recommend using Lokey firmware for production or daily-driver devices yet. This is the first release, so there might be some bugs or rough edges.
Future Goals
The major features that I plan to implement next are (in no particular order):
- Serial communication between device parts (this is required for e.g. wired split keyboards)
- More advanced keyboard functionality like tap-dance behaviour and combos
- Mouse movement reading (optical sensor, touchpad, trackball)
- More MIDI controller functionality
- Better usability and API documentation of the Lokey Editor packages
- Custom bootloader
- Currently the firmware has to be flashed to the device manually (e.g. via a debug probe) or using whatever method the default bootloader of the microcontroller uses. With a custom bootloader it would be possible to update the firmware via Lokey Editor.
With that said, don't expect the fastest development speed as I'm just working on Lokey in my free time. Contributions are very welcome though and will of course help speed things up :)
Lokey in Action
Below are some showcases of Lokey running on a real device.
Sending key codes over Bluetooth.
Remapping keys at runtime.
(The device shown in the videos is a keyboard I'm currently working on but haven't released yet.)
Wrapping Up
Thank you for reading this post :)
If you're interested in trying out Lokey for yourself, you can start by checking out the documentation or the examples in the repository.
Acknowledgements
I want to give a big shout-out to Embassy. Lokey builds on top of many of the Embassy crates and it has made my life a lot easier while developing the framework. Lokey (at least in its current form) wouldn't exist without it.
For the keyboard-related functionality, I took a lot of inspiration from existing keyboard firmware projects like QMK and ZMK.