keyflow 0.1.0

Cross-platform input simulation library for keyboard, mouse and hotkeys.
Documentation
use crate::types::{Button, Key};
use evdev::KeyCode;

/// Converts a [`Key`] to [`KeyCode`]
pub(crate) fn key_to_platform(key: Key) -> Option<KeyCode> {
    match key {
        Key::A => Some(KeyCode::KEY_A),
        Key::B => Some(KeyCode::KEY_B),
        Key::C => Some(KeyCode::KEY_C),
        Key::D => Some(KeyCode::KEY_D),
        Key::E => Some(KeyCode::KEY_E),
        Key::F => Some(KeyCode::KEY_F),
        Key::G => Some(KeyCode::KEY_G),
        Key::H => Some(KeyCode::KEY_H),
        Key::I => Some(KeyCode::KEY_I),
        Key::J => Some(KeyCode::KEY_J),
        Key::K => Some(KeyCode::KEY_K),
        Key::L => Some(KeyCode::KEY_L),
        Key::M => Some(KeyCode::KEY_M),
        Key::N => Some(KeyCode::KEY_N),
        Key::O => Some(KeyCode::KEY_O),
        Key::P => Some(KeyCode::KEY_P),
        Key::Q => Some(KeyCode::KEY_Q),
        Key::R => Some(KeyCode::KEY_R),
        Key::S => Some(KeyCode::KEY_S),
        Key::T => Some(KeyCode::KEY_T),
        Key::U => Some(KeyCode::KEY_U),
        Key::V => Some(KeyCode::KEY_V),
        Key::W => Some(KeyCode::KEY_W),
        Key::X => Some(KeyCode::KEY_X),
        Key::Y => Some(KeyCode::KEY_Y),
        Key::Z => Some(KeyCode::KEY_Z),

        Key::Digit1 => Some(KeyCode::KEY_1),
        Key::Digit2 => Some(KeyCode::KEY_2),
        Key::Digit3 => Some(KeyCode::KEY_3),
        Key::Digit4 => Some(KeyCode::KEY_4),
        Key::Digit5 => Some(KeyCode::KEY_5),
        Key::Digit6 => Some(KeyCode::KEY_6),
        Key::Digit7 => Some(KeyCode::KEY_7),
        Key::Digit8 => Some(KeyCode::KEY_8),
        Key::Digit9 => Some(KeyCode::KEY_9),
        Key::Digit0 => Some(KeyCode::KEY_0),

        Key::F1 => Some(KeyCode::KEY_F1),
        Key::F2 => Some(KeyCode::KEY_F2),
        Key::F3 => Some(KeyCode::KEY_F3),
        Key::F4 => Some(KeyCode::KEY_F4),
        Key::F5 => Some(KeyCode::KEY_F5),
        Key::F6 => Some(KeyCode::KEY_F6),
        Key::F7 => Some(KeyCode::KEY_F7),
        Key::F8 => Some(KeyCode::KEY_F8),
        Key::F9 => Some(KeyCode::KEY_F9),
        Key::F10 => Some(KeyCode::KEY_F10),
        Key::F11 => Some(KeyCode::KEY_F11),
        Key::F12 => Some(KeyCode::KEY_F12),
        Key::F13 => Some(KeyCode::KEY_F13),
        Key::F14 => Some(KeyCode::KEY_F14),
        Key::F15 => Some(KeyCode::KEY_F15),
        Key::F16 => Some(KeyCode::KEY_F16),
        Key::F17 => Some(KeyCode::KEY_F17),
        Key::F18 => Some(KeyCode::KEY_F18),
        Key::F19 => Some(KeyCode::KEY_F19),
        Key::F20 => Some(KeyCode::KEY_F20),
        Key::F21 => Some(KeyCode::KEY_F21),
        Key::F22 => Some(KeyCode::KEY_F22),
        Key::F23 => Some(KeyCode::KEY_F23),
        Key::F24 => Some(KeyCode::KEY_F24),

        Key::ShiftLeft => Some(KeyCode::KEY_LEFTSHIFT),
        Key::ShiftRight => Some(KeyCode::KEY_RIGHTSHIFT),
        Key::ControlLeft => Some(KeyCode::KEY_LEFTCTRL),
        Key::ControlRight => Some(KeyCode::KEY_RIGHTCTRL),
        Key::AltLeft => Some(KeyCode::KEY_LEFTALT),
        Key::AltRight => Some(KeyCode::KEY_RIGHTALT),
        Key::MetaLeft => Some(KeyCode::KEY_LEFTMETA),
        Key::MetaRight => Some(KeyCode::KEY_RIGHTMETA),

        Key::Escape => Some(KeyCode::KEY_ESC),
        Key::Space => Some(KeyCode::KEY_SPACE),
        Key::Enter => Some(KeyCode::KEY_ENTER),
        Key::Tab => Some(KeyCode::KEY_TAB),
        Key::Backspace => Some(KeyCode::KEY_BACKSPACE),
        Key::CapsLock => Some(KeyCode::KEY_CAPSLOCK),
        Key::NumLock => Some(KeyCode::KEY_NUMLOCK),
        Key::ScrollLock => Some(KeyCode::KEY_SCROLLLOCK),

        Key::Backquote => Some(KeyCode::KEY_GRAVE),
        Key::Minus => Some(KeyCode::KEY_MINUS),
        Key::Equal => Some(KeyCode::KEY_EQUAL),
        Key::BracketLeft => Some(KeyCode::KEY_LEFTBRACE),
        Key::BracketRight => Some(KeyCode::KEY_RIGHTBRACE),
        Key::Backslash => Some(KeyCode::KEY_BACKSLASH),
        Key::Semicolon => Some(KeyCode::KEY_SEMICOLON),
        Key::Quote => Some(KeyCode::KEY_APOSTROPHE),
        Key::Comma => Some(KeyCode::KEY_COMMA),
        Key::Period => Some(KeyCode::KEY_DOT),
        Key::Slash => Some(KeyCode::KEY_SLASH),

        Key::IsoBackslash => Some(KeyCode::KEY_102ND),

        Key::Insert => Some(KeyCode::KEY_INSERT),
        Key::Delete => Some(KeyCode::KEY_DELETE),
        Key::Home => Some(KeyCode::KEY_HOME),
        Key::End => Some(KeyCode::KEY_END),
        Key::PageUp => Some(KeyCode::KEY_PAGEUP),
        Key::PageDown => Some(KeyCode::KEY_PAGEDOWN),

        Key::ArrowUp => Some(KeyCode::KEY_UP),
        Key::ArrowDown => Some(KeyCode::KEY_DOWN),
        Key::ArrowLeft => Some(KeyCode::KEY_LEFT),
        Key::ArrowRight => Some(KeyCode::KEY_RIGHT),

        Key::Numpad0 => Some(KeyCode::KEY_KP0),
        Key::Numpad1 => Some(KeyCode::KEY_KP1),
        Key::Numpad2 => Some(KeyCode::KEY_KP2),
        Key::Numpad3 => Some(KeyCode::KEY_KP3),
        Key::Numpad4 => Some(KeyCode::KEY_KP4),
        Key::Numpad5 => Some(KeyCode::KEY_KP5),
        Key::Numpad6 => Some(KeyCode::KEY_KP6),
        Key::Numpad7 => Some(KeyCode::KEY_KP7),
        Key::Numpad8 => Some(KeyCode::KEY_KP8),
        Key::Numpad9 => Some(KeyCode::KEY_KP9),
        Key::NumpadAdd => Some(KeyCode::KEY_KPPLUS),
        Key::NumpadSubtract => Some(KeyCode::KEY_KPMINUS),
        Key::NumpadMultiply => Some(KeyCode::KEY_KPASTERISK),
        Key::NumpadDivide => Some(KeyCode::KEY_KPSLASH),
        Key::NumpadDecimal => Some(KeyCode::KEY_KPDOT),
        Key::NumpadEnter => Some(KeyCode::KEY_KPENTER),
        Key::NumpadEqual => Some(KeyCode::KEY_KPEQUAL),
        Key::NumpadComma => Some(KeyCode::KEY_KPCOMMA),

        Key::PrintScreen => Some(KeyCode::KEY_SYSRQ),
        Key::Pause => Some(KeyCode::KEY_PAUSE),
        Key::ContextMenu => Some(KeyCode::KEY_COMPOSE),
        Key::Power => Some(KeyCode::KEY_POWER),
        Key::Sleep => Some(KeyCode::KEY_SLEEP),
        Key::Wake => Some(KeyCode::KEY_WAKEUP),

        Key::VolumeMute => Some(KeyCode::KEY_MUTE),
        Key::VolumeDown => Some(KeyCode::KEY_VOLUMEDOWN),
        Key::VolumeUp => Some(KeyCode::KEY_VOLUMEUP),
        Key::MediaPlayPause => Some(KeyCode::KEY_PLAYPAUSE),
        Key::MediaStop => Some(KeyCode::KEY_STOPCD),
        Key::MediaTrackNext => Some(KeyCode::KEY_NEXTSONG),
        Key::MediaTrackPrevious => Some(KeyCode::KEY_PREVIOUSSONG),

        Key::BrowserBack => Some(KeyCode::KEY_BACK),
        Key::BrowserForward => Some(KeyCode::KEY_FORWARD),
        Key::BrowserRefresh => Some(KeyCode::KEY_REFRESH),
        Key::BrowserStop => Some(KeyCode::KEY_STOP),
        Key::BrowserSearch => Some(KeyCode::KEY_SEARCH),
        Key::BrowserFavorites => Some(KeyCode::KEY_BOOKMARKS),
        Key::BrowserHome => Some(KeyCode::KEY_HOMEPAGE),

        Key::LaunchMail => Some(KeyCode::KEY_MAIL),
        Key::LaunchMediaPlayer => Some(KeyCode::KEY_MEDIA),
        Key::LaunchApp1 => Some(KeyCode::KEY_PROG1),
        Key::LaunchApp2 => Some(KeyCode::KEY_PROG2),

        Key::NonConvert => Some(KeyCode::KEY_MUHENKAN),
        Key::Convert => Some(KeyCode::KEY_HENKAN),
        Key::KanaMode => Some(KeyCode::KEY_KATAKANAHIRAGANA),
        Key::IntlYen => Some(KeyCode::KEY_YEN),
        Key::IntlRo => Some(KeyCode::KEY_RO),
        Key::Lang1 => Some(KeyCode::KEY_HANGEUL),
        Key::Lang2 => Some(KeyCode::KEY_HANJA),
        Key::Lang3 => Some(KeyCode::KEY_KATAKANA),
        Key::Lang4 => Some(KeyCode::KEY_HIRAGANA),

        Key::Help => Some(KeyCode::KEY_HELP),
        Key::Undo => Some(KeyCode::KEY_UNDO),
        Key::Redo => Some(KeyCode::KEY_REDO),
        Key::Cut => Some(KeyCode::KEY_CUT),
        Key::Copy => Some(KeyCode::KEY_COPY),
        Key::Paste => Some(KeyCode::KEY_PASTE),
        Key::Find => Some(KeyCode::KEY_FIND),
    }
}

/// Converts a [`Button`] to [`KeyCode`]
pub(crate) fn button_to_platform(button: Button) -> Option<KeyCode> {
    match button {
        Button::Left => Some(KeyCode::BTN_LEFT),
        Button::Right => Some(KeyCode::BTN_RIGHT),
        Button::Middle => Some(KeyCode::BTN_MIDDLE),
        Button::Extra1 => Some(KeyCode::BTN_FORWARD),
        Button::Extra2 => Some(KeyCode::BTN_BACK),
        Button::Extra3 => Some(KeyCode::BTN_SIDE),
        Button::Extra4 => Some(KeyCode::BTN_EXTRA),
    }
}

/// Converts a [`KeyCode`] to [`Key`]
pub(crate) fn platform_to_key(kc: KeyCode) -> Option<Key> {
    match kc {
        KeyCode::KEY_A => Some(Key::A),
        KeyCode::KEY_B => Some(Key::B),
        KeyCode::KEY_C => Some(Key::C),
        KeyCode::KEY_D => Some(Key::D),
        KeyCode::KEY_E => Some(Key::E),
        KeyCode::KEY_F => Some(Key::F),
        KeyCode::KEY_G => Some(Key::G),
        KeyCode::KEY_H => Some(Key::H),
        KeyCode::KEY_I => Some(Key::I),
        KeyCode::KEY_J => Some(Key::J),
        KeyCode::KEY_K => Some(Key::K),
        KeyCode::KEY_L => Some(Key::L),
        KeyCode::KEY_M => Some(Key::M),
        KeyCode::KEY_N => Some(Key::N),
        KeyCode::KEY_O => Some(Key::O),
        KeyCode::KEY_P => Some(Key::P),
        KeyCode::KEY_Q => Some(Key::Q),
        KeyCode::KEY_R => Some(Key::R),
        KeyCode::KEY_S => Some(Key::S),
        KeyCode::KEY_T => Some(Key::T),
        KeyCode::KEY_U => Some(Key::U),
        KeyCode::KEY_V => Some(Key::V),
        KeyCode::KEY_W => Some(Key::W),
        KeyCode::KEY_X => Some(Key::X),
        KeyCode::KEY_Y => Some(Key::Y),
        KeyCode::KEY_Z => Some(Key::Z),

        KeyCode::KEY_1 => Some(Key::Digit1),
        KeyCode::KEY_2 => Some(Key::Digit2),
        KeyCode::KEY_3 => Some(Key::Digit3),
        KeyCode::KEY_4 => Some(Key::Digit4),
        KeyCode::KEY_5 => Some(Key::Digit5),
        KeyCode::KEY_6 => Some(Key::Digit6),
        KeyCode::KEY_7 => Some(Key::Digit7),
        KeyCode::KEY_8 => Some(Key::Digit8),
        KeyCode::KEY_9 => Some(Key::Digit9),
        KeyCode::KEY_0 => Some(Key::Digit0),

        KeyCode::KEY_F1 => Some(Key::F1),
        KeyCode::KEY_F2 => Some(Key::F2),
        KeyCode::KEY_F3 => Some(Key::F3),
        KeyCode::KEY_F4 => Some(Key::F4),
        KeyCode::KEY_F5 => Some(Key::F5),
        KeyCode::KEY_F6 => Some(Key::F6),
        KeyCode::KEY_F7 => Some(Key::F7),
        KeyCode::KEY_F8 => Some(Key::F8),
        KeyCode::KEY_F9 => Some(Key::F9),
        KeyCode::KEY_F10 => Some(Key::F10),
        KeyCode::KEY_F11 => Some(Key::F11),
        KeyCode::KEY_F12 => Some(Key::F12),
        KeyCode::KEY_F13 => Some(Key::F13),
        KeyCode::KEY_F14 => Some(Key::F14),
        KeyCode::KEY_F15 => Some(Key::F15),
        KeyCode::KEY_F16 => Some(Key::F16),
        KeyCode::KEY_F17 => Some(Key::F17),
        KeyCode::KEY_F18 => Some(Key::F18),
        KeyCode::KEY_F19 => Some(Key::F19),
        KeyCode::KEY_F20 => Some(Key::F20),
        KeyCode::KEY_F21 => Some(Key::F21),
        KeyCode::KEY_F22 => Some(Key::F22),
        KeyCode::KEY_F23 => Some(Key::F23),
        KeyCode::KEY_F24 => Some(Key::F24),

        KeyCode::KEY_LEFTSHIFT => Some(Key::ShiftLeft),
        KeyCode::KEY_RIGHTSHIFT => Some(Key::ShiftRight),
        KeyCode::KEY_LEFTCTRL => Some(Key::ControlLeft),
        KeyCode::KEY_RIGHTCTRL => Some(Key::ControlRight),
        KeyCode::KEY_LEFTALT => Some(Key::AltLeft),
        KeyCode::KEY_RIGHTALT => Some(Key::AltRight),
        KeyCode::KEY_LEFTMETA => Some(Key::MetaLeft),
        KeyCode::KEY_RIGHTMETA => Some(Key::MetaRight),

        KeyCode::KEY_ESC => Some(Key::Escape),
        KeyCode::KEY_SPACE => Some(Key::Space),
        KeyCode::KEY_ENTER => Some(Key::Enter),
        KeyCode::KEY_TAB => Some(Key::Tab),
        KeyCode::KEY_BACKSPACE => Some(Key::Backspace),
        KeyCode::KEY_CAPSLOCK => Some(Key::CapsLock),
        KeyCode::KEY_NUMLOCK => Some(Key::NumLock),
        KeyCode::KEY_SCROLLLOCK => Some(Key::ScrollLock),

        KeyCode::KEY_GRAVE => Some(Key::Backquote),
        KeyCode::KEY_MINUS => Some(Key::Minus),
        KeyCode::KEY_EQUAL => Some(Key::Equal),
        KeyCode::KEY_LEFTBRACE => Some(Key::BracketLeft),
        KeyCode::KEY_RIGHTBRACE => Some(Key::BracketRight),
        KeyCode::KEY_BACKSLASH => Some(Key::Backslash),
        KeyCode::KEY_SEMICOLON => Some(Key::Semicolon),
        KeyCode::KEY_APOSTROPHE => Some(Key::Quote),
        KeyCode::KEY_COMMA => Some(Key::Comma),
        KeyCode::KEY_DOT => Some(Key::Period),
        KeyCode::KEY_SLASH => Some(Key::Slash),

        KeyCode::KEY_102ND => Some(Key::IsoBackslash),

        KeyCode::KEY_INSERT => Some(Key::Insert),
        KeyCode::KEY_DELETE => Some(Key::Delete),
        KeyCode::KEY_HOME => Some(Key::Home),
        KeyCode::KEY_END => Some(Key::End),
        KeyCode::KEY_PAGEUP => Some(Key::PageUp),
        KeyCode::KEY_PAGEDOWN => Some(Key::PageDown),

        KeyCode::KEY_UP => Some(Key::ArrowUp),
        KeyCode::KEY_DOWN => Some(Key::ArrowDown),
        KeyCode::KEY_LEFT => Some(Key::ArrowLeft),
        KeyCode::KEY_RIGHT => Some(Key::ArrowRight),

        KeyCode::KEY_KP0 => Some(Key::Numpad0),
        KeyCode::KEY_KP1 => Some(Key::Numpad1),
        KeyCode::KEY_KP2 => Some(Key::Numpad2),
        KeyCode::KEY_KP3 => Some(Key::Numpad3),
        KeyCode::KEY_KP4 => Some(Key::Numpad4),
        KeyCode::KEY_KP5 => Some(Key::Numpad5),
        KeyCode::KEY_KP6 => Some(Key::Numpad6),
        KeyCode::KEY_KP7 => Some(Key::Numpad7),
        KeyCode::KEY_KP8 => Some(Key::Numpad8),
        KeyCode::KEY_KP9 => Some(Key::Numpad9),
        KeyCode::KEY_KPPLUS => Some(Key::NumpadAdd),
        KeyCode::KEY_KPMINUS => Some(Key::NumpadSubtract),
        KeyCode::KEY_KPASTERISK => Some(Key::NumpadMultiply),
        KeyCode::KEY_KPSLASH => Some(Key::NumpadDivide),
        KeyCode::KEY_KPDOT => Some(Key::NumpadDecimal),
        KeyCode::KEY_KPENTER => Some(Key::NumpadEnter),
        KeyCode::KEY_KPEQUAL => Some(Key::NumpadEqual),
        KeyCode::KEY_KPCOMMA => Some(Key::NumpadComma),

        KeyCode::KEY_SYSRQ => Some(Key::PrintScreen),
        KeyCode::KEY_PAUSE => Some(Key::Pause),
        KeyCode::KEY_COMPOSE => Some(Key::ContextMenu),
        KeyCode::KEY_POWER => Some(Key::Power),
        KeyCode::KEY_SLEEP => Some(Key::Sleep),
        KeyCode::KEY_WAKEUP => Some(Key::Wake),

        KeyCode::KEY_MUTE => Some(Key::VolumeMute),
        KeyCode::KEY_VOLUMEDOWN => Some(Key::VolumeDown),
        KeyCode::KEY_VOLUMEUP => Some(Key::VolumeUp),
        KeyCode::KEY_PLAYPAUSE => Some(Key::MediaPlayPause),
        KeyCode::KEY_STOPCD => Some(Key::MediaStop),
        KeyCode::KEY_NEXTSONG => Some(Key::MediaTrackNext),
        KeyCode::KEY_PREVIOUSSONG => Some(Key::MediaTrackPrevious),

        KeyCode::KEY_BACK => Some(Key::BrowserBack),
        KeyCode::KEY_FORWARD => Some(Key::BrowserForward),
        KeyCode::KEY_REFRESH => Some(Key::BrowserRefresh),
        KeyCode::KEY_STOP => Some(Key::BrowserStop),
        KeyCode::KEY_SEARCH => Some(Key::BrowserSearch),
        KeyCode::KEY_BOOKMARKS => Some(Key::BrowserFavorites),
        KeyCode::KEY_HOMEPAGE => Some(Key::BrowserHome),

        KeyCode::KEY_MAIL => Some(Key::LaunchMail),
        KeyCode::KEY_MEDIA => Some(Key::LaunchMediaPlayer),
        KeyCode::KEY_PROG1 => Some(Key::LaunchApp1),
        KeyCode::KEY_PROG2 => Some(Key::LaunchApp2),

        KeyCode::KEY_MUHENKAN => Some(Key::NonConvert),
        KeyCode::KEY_HENKAN => Some(Key::Convert),
        KeyCode::KEY_KATAKANAHIRAGANA => Some(Key::KanaMode),
        KeyCode::KEY_YEN => Some(Key::IntlYen),
        KeyCode::KEY_RO => Some(Key::IntlRo),
        KeyCode::KEY_HANGEUL => Some(Key::Lang1),
        KeyCode::KEY_HANJA => Some(Key::Lang2),
        KeyCode::KEY_KATAKANA => Some(Key::Lang3),
        KeyCode::KEY_HIRAGANA => Some(Key::Lang4),

        KeyCode::KEY_HELP => Some(Key::Help),
        KeyCode::KEY_UNDO => Some(Key::Undo),
        KeyCode::KEY_REDO => Some(Key::Redo),
        KeyCode::KEY_CUT => Some(Key::Cut),
        KeyCode::KEY_COPY => Some(Key::Copy),
        KeyCode::KEY_PASTE => Some(Key::Paste),
        KeyCode::KEY_FIND => Some(Key::Find),
        _ => None,
    }
}

/// Converts a [`KeyCode`] to [`Button`]
#[allow(unused)]
pub(crate) fn platform_to_button(kc: KeyCode) -> Option<Button> {
    match kc {
        KeyCode::BTN_LEFT => Some(Button::Left),
        KeyCode::BTN_RIGHT => Some(Button::Right),
        KeyCode::BTN_MIDDLE => Some(Button::Middle),
        KeyCode::BTN_FORWARD => Some(Button::Extra1),
        KeyCode::BTN_BACK => Some(Button::Extra2),
        KeyCode::BTN_SIDE => Some(Button::Extra3),
        KeyCode::BTN_EXTRA => Some(Button::Extra4),
        _ => None,
    }
}