Crate keyboard_codes

Crate keyboard_codes 

Source
Expand description

keyboard-codes: Cross-platform keyboard key code mapping and conversion

This crate provides comprehensive keyboard key definitions and cross-platform code mapping for Windows, Linux, and macOS. It supports standard keys, modifiers, custom key mapping, and bidirectional conversion between key names and platform-specific codes.

§Examples

use keyboard_codes::{Key, Modifier, Platform, KeyCodeMapper, KeyboardInput};

// Parse key from string
let key: Key = "Enter".parse().unwrap();
assert_eq!(key, Key::Enter);

// Convert key to platform-specific code
let windows_code = key.to_code(Platform::Windows);
let linux_code = key.to_code(Platform::Linux);

// Parse key from code
let key_from_code = Key::from_code(0x0D, Platform::Windows).unwrap();
assert_eq!(key_from_code, Key::Enter);

// Unified keyboard input handling
let input1: KeyboardInput = "A".parse().unwrap();        // Key
let input2: KeyboardInput = "Ctrl".parse().unwrap();     // Modifier
let input3: KeyboardInput = "ctrl".parse().unwrap();     // Modifier with alias

// Unified code conversion
let code1 = input1.to_code(Platform::Windows);
let code2 = input2.to_code(Platform::Windows);

§Features

  • serde: Enables serialization/deserialization support
  • phf: Uses perfect hash functions for faster lookups

Re-exports§

pub use error::KeyParseError;
pub use mapping::custom::CustomKey;
pub use mapping::custom::CustomKeyMap;
pub use types::Key;
pub use types::KeyCodeMapper;
pub use types::KeyboardInput;
pub use types::Modifier;
pub use types::Platform;
pub use mapping::standard::parse_key_ignore_case;
pub use mapping::standard::parse_modifier_ignore_case;
pub use parser::parse_input;
pub use parser::parse_keyboard_input;
pub use parser::parse_modifier_with_aliases;
pub use parser::parse_shortcut_flexible;
pub use parser::parse_shortcut_sequence;
pub use parser::parse_shortcut_with_aliases;
pub use parser::Shortcut;

Modules§

error
Error types for keyboard parsing and mapping
mapping
Key code mapping implementations Key code mapping implementations
parser
Advanced keyboard input parsing with alias support Advanced keyboard input parsing with alias support and flexible formatting
types
Core type definitions for keyboard keys and platforms Core type definitions for keyboard keys and platforms
utils
Utility functions and helpers Utility functions and helpers

Functions§

current_platform
Get the current platform based on compilation target