monio 0.1.1

Pure Rust cross-platform input monitoring library with proper drag detection
Documentation
//! Windows VK code to Key mappings.

use crate::keycode::Key;

/// Convert a Windows VK code to our Key enum.
pub fn keycode_to_key(code: u16) -> Key {
    match code {
        // Letters (A-Z are 0x41-0x5A)
        0x41 => Key::KeyA,
        0x42 => Key::KeyB,
        0x43 => Key::KeyC,
        0x44 => Key::KeyD,
        0x45 => Key::KeyE,
        0x46 => Key::KeyF,
        0x47 => Key::KeyG,
        0x48 => Key::KeyH,
        0x49 => Key::KeyI,
        0x4A => Key::KeyJ,
        0x4B => Key::KeyK,
        0x4C => Key::KeyL,
        0x4D => Key::KeyM,
        0x4E => Key::KeyN,
        0x4F => Key::KeyO,
        0x50 => Key::KeyP,
        0x51 => Key::KeyQ,
        0x52 => Key::KeyR,
        0x53 => Key::KeyS,
        0x54 => Key::KeyT,
        0x55 => Key::KeyU,
        0x56 => Key::KeyV,
        0x57 => Key::KeyW,
        0x58 => Key::KeyX,
        0x59 => Key::KeyY,
        0x5A => Key::KeyZ,

        // Numbers (0-9 are 0x30-0x39)
        0x30 => Key::Num0,
        0x31 => Key::Num1,
        0x32 => Key::Num2,
        0x33 => Key::Num3,
        0x34 => Key::Num4,
        0x35 => Key::Num5,
        0x36 => Key::Num6,
        0x37 => Key::Num7,
        0x38 => Key::Num8,
        0x39 => Key::Num9,

        // Function keys
        0x70 => Key::F1,
        0x71 => Key::F2,
        0x72 => Key::F3,
        0x73 => Key::F4,
        0x74 => Key::F5,
        0x75 => Key::F6,
        0x76 => Key::F7,
        0x77 => Key::F8,
        0x78 => Key::F9,
        0x79 => Key::F10,
        0x7A => Key::F11,
        0x7B => Key::F12,
        0x7C => Key::F13,
        0x7D => Key::F14,
        0x7E => Key::F15,
        0x7F => Key::F16,
        0x80 => Key::F17,
        0x81 => Key::F18,
        0x82 => Key::F19,
        0x83 => Key::F20,
        0x84 => Key::F21,
        0x85 => Key::F22,
        0x86 => Key::F23,
        0x87 => Key::F24,

        // Modifiers
        0xA0 => Key::ShiftLeft,
        0xA1 => Key::ShiftRight,
        0xA2 => Key::ControlLeft,
        0xA3 => Key::ControlRight,
        0xA4 => Key::AltLeft,
        0xA5 => Key::AltRight,
        0x5B => Key::MetaLeft,
        0x5C => Key::MetaRight,

        // Navigation and special
        0x08 => Key::Backspace,
        0x09 => Key::Tab,
        0x0D => Key::Enter,
        0x14 => Key::CapsLock,
        0x1B => Key::Escape,
        0x20 => Key::Space,
        0x21 => Key::PageUp,
        0x22 => Key::PageDown,
        0x23 => Key::End,
        0x24 => Key::Home,
        0x25 => Key::ArrowLeft,
        0x26 => Key::ArrowUp,
        0x27 => Key::ArrowRight,
        0x28 => Key::ArrowDown,
        0x2D => Key::Insert,
        0x2E => Key::Delete,

        // Lock keys
        0x90 => Key::NumLock,
        0x91 => Key::ScrollLock,
        0x2C => Key::PrintScreen,
        0x13 => Key::Pause,

        // Punctuation
        0xC0 => Key::Grave,
        0xBD => Key::Minus,
        0xBB => Key::Equal,
        0xDB => Key::BracketLeft,
        0xDD => Key::BracketRight,
        0xDC => Key::Backslash,
        0xBA => Key::Semicolon,
        0xDE => Key::Quote,
        0xBC => Key::Comma,
        0xBE => Key::Period,
        0xBF => Key::Slash,
        0xE2 => Key::IntlBackslash,

        // Numpad
        0x60 => Key::Numpad0,
        0x61 => Key::Numpad1,
        0x62 => Key::Numpad2,
        0x63 => Key::Numpad3,
        0x64 => Key::Numpad4,
        0x65 => Key::Numpad5,
        0x66 => Key::Numpad6,
        0x67 => Key::Numpad7,
        0x68 => Key::Numpad8,
        0x69 => Key::Numpad9,
        0x6A => Key::NumpadMultiply,
        0x6B => Key::NumpadAdd,
        0x6D => Key::NumpadSubtract,
        0x6E => Key::NumpadDecimal,
        0x6F => Key::NumpadDivide,

        // Media keys
        0xAD => Key::VolumeMute,
        0xAE => Key::VolumeDown,
        0xAF => Key::VolumeUp,
        0xB0 => Key::MediaNext,
        0xB1 => Key::MediaPrevious,
        0xB2 => Key::MediaStop,
        0xB3 => Key::MediaPlayPause,

        // Browser keys
        0xA6 => Key::BrowserBack,
        0xA7 => Key::BrowserForward,
        0xA8 => Key::BrowserRefresh,
        0xA9 => Key::BrowserStop,
        0xAA => Key::BrowserSearch,
        0xAB => Key::BrowserFavorites,
        0xAC => Key::BrowserHome,

        // Application keys
        0x5D => Key::ContextMenu,

        _ => Key::Unknown(code as u32),
    }
}

/// Convert our Key enum to a Windows VK code.
pub fn key_to_keycode(key: Key) -> Option<u16> {
    Some(match key {
        // Letters
        Key::KeyA => 0x41,
        Key::KeyB => 0x42,
        Key::KeyC => 0x43,
        Key::KeyD => 0x44,
        Key::KeyE => 0x45,
        Key::KeyF => 0x46,
        Key::KeyG => 0x47,
        Key::KeyH => 0x48,
        Key::KeyI => 0x49,
        Key::KeyJ => 0x4A,
        Key::KeyK => 0x4B,
        Key::KeyL => 0x4C,
        Key::KeyM => 0x4D,
        Key::KeyN => 0x4E,
        Key::KeyO => 0x4F,
        Key::KeyP => 0x50,
        Key::KeyQ => 0x51,
        Key::KeyR => 0x52,
        Key::KeyS => 0x53,
        Key::KeyT => 0x54,
        Key::KeyU => 0x55,
        Key::KeyV => 0x56,
        Key::KeyW => 0x57,
        Key::KeyX => 0x58,
        Key::KeyY => 0x59,
        Key::KeyZ => 0x5A,

        // Numbers
        Key::Num0 => 0x30,
        Key::Num1 => 0x31,
        Key::Num2 => 0x32,
        Key::Num3 => 0x33,
        Key::Num4 => 0x34,
        Key::Num5 => 0x35,
        Key::Num6 => 0x36,
        Key::Num7 => 0x37,
        Key::Num8 => 0x38,
        Key::Num9 => 0x39,

        // Function keys
        Key::F1 => 0x70,
        Key::F2 => 0x71,
        Key::F3 => 0x72,
        Key::F4 => 0x73,
        Key::F5 => 0x74,
        Key::F6 => 0x75,
        Key::F7 => 0x76,
        Key::F8 => 0x77,
        Key::F9 => 0x78,
        Key::F10 => 0x79,
        Key::F11 => 0x7A,
        Key::F12 => 0x7B,
        Key::F13 => 0x7C,
        Key::F14 => 0x7D,
        Key::F15 => 0x7E,
        Key::F16 => 0x7F,
        Key::F17 => 0x80,
        Key::F18 => 0x81,
        Key::F19 => 0x82,
        Key::F20 => 0x83,
        Key::F21 => 0x84,
        Key::F22 => 0x85,
        Key::F23 => 0x86,
        Key::F24 => 0x87,

        // Modifiers
        Key::ShiftLeft => 0xA0,
        Key::ShiftRight => 0xA1,
        Key::ControlLeft => 0xA2,
        Key::ControlRight => 0xA3,
        Key::AltLeft => 0xA4,
        Key::AltRight => 0xA5,
        Key::MetaLeft => 0x5B,
        Key::MetaRight => 0x5C,

        // Navigation and special
        Key::Backspace => 0x08,
        Key::Tab => 0x09,
        Key::Enter => 0x0D,
        Key::CapsLock => 0x14,
        Key::Escape => 0x1B,
        Key::Space => 0x20,
        Key::PageUp => 0x21,
        Key::PageDown => 0x22,
        Key::End => 0x23,
        Key::Home => 0x24,
        Key::ArrowLeft => 0x25,
        Key::ArrowUp => 0x26,
        Key::ArrowRight => 0x27,
        Key::ArrowDown => 0x28,
        Key::Insert => 0x2D,
        Key::Delete => 0x2E,

        // Lock keys
        Key::NumLock => 0x90,
        Key::ScrollLock => 0x91,
        Key::PrintScreen => 0x2C,
        Key::Pause => 0x13,

        // Punctuation
        Key::Grave => 0xC0,
        Key::Minus => 0xBD,
        Key::Equal => 0xBB,
        Key::BracketLeft => 0xDB,
        Key::BracketRight => 0xDD,
        Key::Backslash => 0xDC,
        Key::Semicolon => 0xBA,
        Key::Quote => 0xDE,
        Key::Comma => 0xBC,
        Key::Period => 0xBE,
        Key::Slash => 0xBF,
        Key::IntlBackslash => 0xE2,

        // Numpad
        Key::Numpad0 => 0x60,
        Key::Numpad1 => 0x61,
        Key::Numpad2 => 0x62,
        Key::Numpad3 => 0x63,
        Key::Numpad4 => 0x64,
        Key::Numpad5 => 0x65,
        Key::Numpad6 => 0x66,
        Key::Numpad7 => 0x67,
        Key::Numpad8 => 0x68,
        Key::Numpad9 => 0x69,
        Key::NumpadMultiply => 0x6A,
        Key::NumpadAdd => 0x6B,
        Key::NumpadSubtract => 0x6D,
        Key::NumpadDecimal => 0x6E,
        Key::NumpadDivide => 0x6F,

        // Media keys
        Key::VolumeMute => 0xAD,
        Key::VolumeDown => 0xAE,
        Key::VolumeUp => 0xAF,
        Key::MediaNext => 0xB0,
        Key::MediaPrevious => 0xB1,
        Key::MediaStop => 0xB2,
        Key::MediaPlayPause => 0xB3,

        // Browser keys
        Key::BrowserBack => 0xA6,
        Key::BrowserForward => 0xA7,
        Key::BrowserRefresh => 0xA8,
        Key::BrowserStop => 0xA9,
        Key::BrowserSearch => 0xAA,
        Key::BrowserFavorites => 0xAB,
        Key::BrowserHome => 0xAC,

        // Application keys
        Key::ContextMenu => 0x5D,

        Key::Unknown(code) => code as u16,
        _ => return None,
    })
}