From b2d074e5e59158fb0bd4bad032f48623e6fa3266 Mon Sep 17 00:00:00 2001 From: XX Date: Mon, 18 Feb 2019 18:15:23 +0300 Subject: [PATCH] Share key labels --- Cargo.lock | 34 +++++++++++ Cargo.toml | 4 ++ src/system.rs | 73 ++++++++++++++++++++++- src/system/{linux.rs => unix.rs} | 0 src/system/{linux => unix}/input.rs | 90 ++++++++++++++--------------- 5 files changed, 153 insertions(+), 48 deletions(-) rename src/system/{linux.rs => unix.rs} (100%) rename src/system/{linux => unix}/input.rs (52%) diff --git a/Cargo.lock b/Cargo.lock index 9f01cdc..d85a9a9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -59,6 +59,15 @@ dependencies = [ "quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "kernel32-sys" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "keylogger" version = "0.2.0" @@ -66,8 +75,10 @@ dependencies = [ "chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "getopts 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)", + "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "user32-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -190,11 +201,25 @@ name = "unicode-width" version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "user32-sys" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "utf8-ranges" version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "winapi" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "winapi" version = "0.3.6" @@ -204,6 +229,11 @@ dependencies = [ "winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "winapi-build" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "winapi-i686-pc-windows-gnu" version = "0.4.0" @@ -239,6 +269,7 @@ dependencies = [ "checksum env_logger 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "afb070faf94c85d17d50ca44f6ad076bce18ae92f0037d350947240a36e9d42e" "checksum getopts 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)" = "0a7292d30132fb5424b354f5dc02512a86e4c516fe544bb7a25e7f266951b797" "checksum humantime 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3ca7e5f2e110db35f93b837c81797f3714500b81d517bf20c431b16d3ca4f114" +"checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" "checksum lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a374c89b9db55895453a74c1e38861d9deec0b01b405a82516e9d5de4820dea1" "checksum libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)" = "e962c7641008ac010fa60a7dfdc1712449f29c44ef2d4702394aea943ee75047" "checksum log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c84ec4b527950aa83a329754b01dbe3f58361d1c5efacd1f6d68c494d08a17c6" @@ -256,8 +287,11 @@ dependencies = [ "checksum time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "db8dcfca086c1143c9270ac42a2bbd8a7ee477b78ac8e45b19abfb0cbede4b6f" "checksum ucd-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "535c204ee4d8434478593480b8f86ab45ec9aae0e83c568ca81abf0fd0e88f86" "checksum unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "882386231c45df4700b275c7ff55b6f3698780a650026380e72dabe76fa46526" +"checksum user32-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4ef4711d107b21b410a3a974b1204d9accc8b10dad75d8324b5d755de1617d47" "checksum utf8-ranges 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "796f7e48bef87609f7ade7e06495a87d5cd06c7866e6a5cbfceffc558a243737" +"checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" "checksum winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "92c1eb33641e276cfa214a0522acad57be5c56b10cb348b3c5117db75f3ac4b0" +"checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc" "checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" "checksum winapi-util 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7168bab6e1daee33b4557efd0e95d5ca70a03706d39fa5f3fe7a236f584b03c9" "checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/Cargo.toml b/Cargo.toml index dc6e37a..88fb29e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,3 +13,7 @@ libc = "0.2" env_logger = "0.6" log = "0.4" chrono = "0.4" + +[target.'cfg(windows)'.dependencies] +user32-sys = "0.2" +kernel32-sys = "0.2" \ No newline at end of file diff --git a/src/system.rs b/src/system.rs index ee3aa0a..05188a0 100644 --- a/src/system.rs +++ b/src/system.rs @@ -1,7 +1,15 @@ -pub mod linux; +#[cfg(unix)] +pub mod unix; + +#[cfg(windows)] pub mod windows; -pub use self::linux::*; +#[cfg(unix)] +pub use self::unix::*; + +#[cfg(windows)] +pub use self::windows::*; + #[derive(Copy, Clone)] pub enum PressEvent { @@ -16,4 +24,65 @@ impl PressEvent { PressEvent::Release => "RE", } } +} + +pub struct Key; + +impl Key { + // Unknown key string + pub const UK: &'static str = ""; + + pub const ESC: &'static str = ""; + pub const BACKSPACE: &'static str = ""; + pub const TAB: &'static str = ""; + pub const ENTER: &'static str = ""; + pub const LCTRL: &'static str = ""; + pub const RCTRL: &'static str = ""; + pub const LSHIFT: &'static str = ""; + pub const RSHIFT: &'static str = ""; + pub const LALT: &'static str = ""; + pub const RALT: &'static str = ""; + pub const SPACE: &'static str = ""; + pub const F1: &'static str = ""; + pub const F2: &'static str = ""; + pub const F3: &'static str = ""; + pub const F4: &'static str = ""; + pub const F5: &'static str = ""; + pub const F6: &'static str = ""; + pub const F7: &'static str = ""; + pub const F8: &'static str = ""; + pub const F9: &'static str = ""; + pub const F10: &'static str = ""; + pub const F11: &'static str = ""; + pub const F12: &'static str = ""; + pub const UP: &'static str = ""; + pub const LEFT: &'static str = ""; + pub const RIGHT: &'static str = ""; + pub const DOWN: &'static str = ""; + pub const HOME: &'static str = ""; + pub const END: &'static str = ""; + pub const PAGE_UP: &'static str = ""; + pub const PAGE_DOWN: &'static str = ""; + pub const INSERT: &'static str = ""; + pub const DELETE: &'static str = ""; + pub const SYS_RQ: &'static str = ""; + pub const CAPS_LOCK: &'static str = ""; + pub const SCROL_LOCK: &'static str = ""; + pub const NUM_LOCK: &'static str = ""; + pub const KP0: &'static str = ""; + pub const KP1: &'static str = ""; + pub const KP2: &'static str = ""; + pub const KP3: &'static str = ""; + pub const KP4: &'static str = ""; + pub const KP5: &'static str = ""; + pub const KP6: &'static str = ""; + pub const KP7: &'static str = ""; + pub const KP8: &'static str = ""; + pub const KP9: &'static str = ""; + pub const KP_STAR: &'static str = ""; + pub const KP_DIV: &'static str = ""; + pub const KP_ADD: &'static str = ""; + pub const KP_SUB: &'static str = ""; + pub const KP_POINT: &'static str = ""; + pub const KP_ENTER: &'static str = ""; } \ No newline at end of file diff --git a/src/system/linux.rs b/src/system/unix.rs similarity index 100% rename from src/system/linux.rs rename to src/system/unix.rs diff --git a/src/system/linux/input.rs b/src/system/unix/input.rs similarity index 52% rename from src/system/linux/input.rs rename to src/system/unix/input.rs index 0ec7e06..892a044 100644 --- a/src/system/linux/input.rs +++ b/src/system/unix/input.rs @@ -1,5 +1,6 @@ // Constants, structs, and arrays derived from /linux/include/linux/input.h use log::debug; +use crate::system::Key; const MAX_KEYS: u16 = 112; @@ -21,63 +22,60 @@ pub struct InputEvent { pub value: i32 } -// Unknown key string -const UK: &'static str = ""; - const KEY_NAMES: [&'static str; MAX_KEYS as usize] = [ - UK, "", + Key::UK, Key::ESC, "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "=", - "", "", + Key::BACKSPACE, Key::TAB, "q", "w", "e", "r", "t", "y", "u", "i", "o", "p", - "[", "]", "", "", + "[", "]", Key::ENTER, Key::LCTRL, "a", "s", "d", "f", "g", "h", "j", "k", "l", ";", - "'", "`", "", + "'", "`", Key::LSHIFT, "\\", "z", "x", "c", "v", "b", "n", "m", ",", ".", "/", - "", - "", - "", " ", "", - "", "", "", "", "", "", "", "", "", "", - "", "", - "", "", "", - "", - "", "", "", - "", - "", "", "", "", - "", - UK, UK, UK, - "", "", - UK, UK, UK, UK, UK, UK, UK, - "", "", "", "", "", UK, - "", "", "", "", "", "", "", - "", "", "" + Key::RSHIFT, + Key::KP_STAR, + Key::LALT, Key::SPACE, Key::CAPS_LOCK, + Key::F1, Key::F2, Key::F3, Key::F4, Key::F5, Key::F6, Key::F7, Key::F8, Key::F9, Key::F10, + Key::NUM_LOCK, Key::SCROL_LOCK, + Key::KP7, Key::KP8, Key::KP9, + Key::KP_SUB, + Key::KP4, Key::KP5, Key::KP6, + Key::KP_ADD, + Key::KP1, Key::KP2, Key::KP3, Key::KP0, + Key::KP_POINT, + Key::UK, Key::UK, Key::UK, + Key::F11, Key::F12, + Key::UK, Key::UK, Key::UK, Key::UK, Key::UK, Key::UK, Key::UK, + Key::KP_ENTER, Key::RCTRL, Key::KP_DIV, Key::SYS_RQ, Key::RALT, Key::UK, + Key::HOME, Key::UP, Key::PAGE_UP, Key::LEFT, Key::RIGHT, Key::END, Key::DOWN, + Key::PAGE_DOWN, Key::INSERT, Key::DELETE ]; const SHIFT_KEY_NAMES: [&'static str; MAX_KEYS as usize] = [ - UK, "", + Key::UK, Key::ESC, "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "+", - "", "", + Key::BACKSPACE, Key::TAB, "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", - "{", "}", "", "", + "{", "}", Key::ENTER, Key::LCTRL, "A", "S", "D", "F", "G", "H", "J", "K", "L", ":", - "\"", "~", "", + "\"", "~", Key::LSHIFT, "|", "Z", "X", "C", "V", "B", "N", "M", "<", ">", "?", - "", - "", - "", " ", "", - "", "", "", "", "", "", "", "", "", "", - "", "", - "", "", "", - "", - "", "", "", - "", - "", "", "", "", - "", - UK, UK, UK, - "", "", - UK, UK, UK, UK, UK, UK, UK, - "", "", "", "", "", UK, - "", "", "", "", "", "", "", - "", "", "" + Key::RSHIFT, + Key::KP_STAR, + Key::LALT, Key::SPACE, Key::CAPS_LOCK, + Key::F1, Key::F2, Key::F3, Key::F4, Key::F5, Key::F6, Key::F7, Key::F8, Key::F9, Key::F10, + Key::NUM_LOCK, Key::SCROL_LOCK, + Key::KP7, Key::KP8, Key::KP9, + Key::KP_SUB, + Key::KP4, Key::KP5, Key::KP6, + Key::KP_ADD, + Key::KP1, Key::KP2, Key::KP3, Key::KP0, + Key::KP_POINT, + Key::UK, Key::UK, Key::UK, + Key::F11, Key::F12, + Key::UK, Key::UK, Key::UK, Key::UK, Key::UK, Key::UK, Key::UK, + Key::KP_ENTER, Key::RCTRL, Key::KP_DIV, Key::SYS_RQ, Key::RALT, Key::UK, + Key::HOME, Key::UP, Key::PAGE_UP, Key::LEFT, Key::RIGHT, Key::END, Key::DOWN, + Key::PAGE_DOWN, Key::INSERT, Key::DELETE ]; // Converts a key code to it's ascii representation. Some unprintable keys like escape are printed @@ -93,7 +91,7 @@ pub fn get_key_text(code: u16, shift_pressed: u8) -> &'static str { return arr[code as usize]; } else { debug!("Unknown key: {}", code); - return UK; + return Key::UK; } }