Coder Social home page Coder Social logo

keyboard-layouts's Introduction

Keyboard Layouts

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.

Example Usage

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);

Virtual Keyboard Device

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:

  • Create a HID gadget device file on a Linux SBC with an OTG USB port. E.g. Raspberry Pi, Beaglebone. This guide describes how
  • Check out the tests to see how to use the tokio-linux-uhid crate to create a virtual HID device on a Linux desktop

I'm afraid for Windows and Mac I have no idea.

kbsim CLI tool

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>  

Supported Layouts

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

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:

  1. Sets the user session's keyboard layout (only in plain virtual console, no X)
  2. Creates a virtual HID device on the machine using /dev/uhid (user needs permissions)
  3. Writes all the specified characters to the virtual HID device (cursor needs to be in the testing terminal and stay there)
  4. Reads the string of types from stdin and compares with the original.

keyboard-layouts's People

Contributors

chris-ricketts avatar colemickens avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

keyboard-layouts's Issues

Cargo.lock is missing

This prevents me from packaging it for nixpkgs. It would be great to have an easily-deployed virtual keyboard software to run on RPi0s. With NixOS especially, some interesting little things can be built.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.