Crates.io | keyboard-layouts |
lib.rs | keyboard-layouts |
version | 0.3.0 |
source | src |
created_at | 2019-02-28 13:03:54.653942 |
updated_at | 2020-08-03 23:12:24.397404 |
description | Keyboard layout keymaps with an API to generate HID packets |
homepage | |
repository | https://github.com/Chris-Ricketts/keyboard-layouts |
max_upload_size | |
id | 117771 |
size | 69,836 |
Get the keycodes and modifier keys required to type an ASCII string for a number of different keyboard layouts.
Takes inspiration and the initial layout mappings from the Teensyduino project.
It works by preprocessing a C header file that describes the key mappings for each layout, including any deadkeys using #define
's. It then uses bindgen to convert those into Rust constants and then syn to extract the relevant keycodes and masks. It finally uses quote! and lazystatic! to produce a layout map enabling you to switch keyboard layouts on the fly without recompilation.
let test_string = "This is a test string.\n";
// Get the sequence of HID packets that would be produced by a keyboard with the specified layout
let hid_packets = keyboard_layouts::string_to_hid_packets("LAYOUT_UNITED_KINGDOM", test_string).unwrap();
// Write those HID packets to your virtual keyboard device. In this case a OTG HID gadget device file (linux).
std::fs::write("/dev/hidg0", hid_packets);
This depends on your operating system and underlying hardware. So far this has only been tried on Linux but the HID packets should be valid for Windows and Mac.
On Linux you can either:
I'm afraid for Windows and Mac I have no idea.
There is a CLI tool, kbsim
, included that can be useful.
USAGE:
kbsim [FLAGS] [OPTIONS] [STRING]
FLAGS:
-h, --help Prints help information
-n, --newline Hit the 'Enter' key after writing the string
-V, --version Prints version information
OPTIONS:
-c, --cooldown <cooldown> Specify the number of milliseconds to wait between sending each HID packet to the
device file [default: 0]
-d, --delay <delay> Specify the number of seconds to wait before writing [default: 0]
-f, --hid-file <hid_file> The HID file to write to. Defaults to /dev/hidg0
-l, --layout <layout> The keyboard layout to use. Specify 'list' to show all available layouts [default:
LAYOUT_US_ENGLISH]
ARGS:
<STRING>
Spanish
Canadian French
German Mac
German Swiss
Icelandic
United Kingdom
Italian
French Swiss
Finnish
Danish
French
German
Turkish
French Belgian
Portuguese
Canadian Multilingual
Spanish Latin America
US English
US International
Swedish
Portuguese Brazilian
Irish
Norwegian
Testing all the layouts are correct is hard. As a result the tests are hacky.
Testing for each layout is split into alphanumeric and symbols. Each test: