GitXplorerGitXplorer
d

keycode

public
9 stars
6 forks
6 issues

Commits

List of commits on branch master.
Unverified
fc4f2fc6ef24be54de508cee474354a2f2216e71

v0.4.0

ddfrankland committed 2 years ago
Unverified
a5f786e9d6d83cbddbcc0e264039b9db5560c109

added optional serde support

MMoxinilian committed 2 years ago
Unverified
bf5b21bf89acebf40c26e81b82d65b147b31fe9e

Fix more clippy errors and warnings

ddfrankland committed 5 years ago
Unverified
bda7142f3b8f0d4db9844dcd90a4691bcd1dc654

Fix clippy errors and warnings

ddfrankland committed 5 years ago
Unverified
8aa9c8e0163c4f93ef57dc646298cb32ee8f0f70

Remove build scripts and parse keycode_converter_data.inc as string in macro

ddfrankland committed 5 years ago
Unverified
7ffd6ec6b007c684f7d33b778dd4ee734885eee1

Symlink keycode_converter_data.inc

ddfrankland committed 5 years ago

README

The README file for this repository.

keycode

A Rust crate for translating keycodes based on Chrome's mapping of keys.

Easily convert, generate, listen for, or map keycodes for Linux, Windows, Mac, USB, and browsers! Includes a struct to manage the state of pressed keys and generate USB HID reports. Can be used for #![no_std] crates.

Source Data

Source of keycodes data:

How to update source file:

curl -sL 'https://chromium.googlesource.com/chromium/src/+/master/ui/events/keycodes/dom/keycode_converter_data.inc?format=TEXT' | base64 --decode > keycode_converter_data.inc

Examples

Get a key mapping

use keycode::{KeyMap, KeyMappingId};

// Check the USB HID value of the "a" key
fn main() {
    let a = KeyMap::from(KeyMappingId::UsA);
    assert_eq!(a.usb, 0x0004);
    assert_eq!(a.evdev, 0x001e);
    assert_eq!(a.xkb, 0x0026);
    assert_eq!(a.win, 0x001e);
    assert_eq!(a.mac, 0x0000);
}

Generate a USB HID report

use keycode::{KeyboardState, KeyMap, KeyMappingId, KeyState};

// Press and release the "A" key
fn main() {
    // Generate a keyboard state with n-key rollover
    let mut keyboard_state = KeyboardState::new(None);

    // Get key mappings
    let a = KeyMap::from(KeyMappingId::UsA);
    let shift = KeyMap::from(KeyMappingId::ShiftLeft);

    // USB HID report for "no keys pressed"
    assert_eq!(keyboard_state.usb_input_report(), &[0; 8]);

    // Press "shift" and "a" keys
    keyboard_state.update_key(a, KeyState::Pressed);
    keyboard_state.update_key(shift, KeyState::Pressed);

    // USB HID report for "'A' is pressed"
    assert_eq!(
        keyboard_state.usb_input_report(),
        &[shift.modifier.unwrap().bits(), 0, a.usb as u8, 0, 0, 0, 0, 0]
    );

    // Release "shift" and "a" keys
    keyboard_state.update_key(a, KeyState::Released);
    keyboard_state.update_key(shift, KeyState::Released);

    // USB HID report for "no keys pressed"
    assert_eq!(keyboard_state.usb_input_report(), &[0; 8]);
}

Supported Rust Versions

Requires Rust 1.34.0 or newer due to use of TryFrom.