Skip to content

Commit

Permalink
Share key labels
Browse files Browse the repository at this point in the history
  • Loading branch information
XX committed Feb 18, 2019
1 parent aa896e0 commit b2d074e
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 48 deletions.
34 changes: 34 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
73 changes: 71 additions & 2 deletions src/system.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -16,4 +24,65 @@ impl PressEvent {
PressEvent::Release => "RE",
}
}
}

pub struct Key;

impl Key {
// Unknown key string
pub const UK: &'static str = "<UK>";

pub const ESC: &'static str = "<ESC>";
pub const BACKSPACE: &'static str = "<Backspace>";
pub const TAB: &'static str = "<Tab>";
pub const ENTER: &'static str = "<Enter>";
pub const LCTRL: &'static str = "<LCtrl>";
pub const RCTRL: &'static str = "<RCtrl>";
pub const LSHIFT: &'static str = "<LShift>";
pub const RSHIFT: &'static str = "<RShift>";
pub const LALT: &'static str = "<LAlt>";
pub const RALT: &'static str = "<RAlt>";
pub const SPACE: &'static str = "<Space>";
pub const F1: &'static str = "<F1>";
pub const F2: &'static str = "<F2>";
pub const F3: &'static str = "<F3>";
pub const F4: &'static str = "<F4>";
pub const F5: &'static str = "<F5>";
pub const F6: &'static str = "<F6>";
pub const F7: &'static str = "<F7>";
pub const F8: &'static str = "<F8>";
pub const F9: &'static str = "<F9>";
pub const F10: &'static str = "<F10>";
pub const F11: &'static str = "<F11>";
pub const F12: &'static str = "<F12>";
pub const UP: &'static str = "<Up>";
pub const LEFT: &'static str = "<Left>";
pub const RIGHT: &'static str = "<Right>";
pub const DOWN: &'static str = "<Down>";
pub const HOME: &'static str = "<Home>";
pub const END: &'static str = "<End>";
pub const PAGE_UP: &'static str = "<PageUp>";
pub const PAGE_DOWN: &'static str = "<PageDown>";
pub const INSERT: &'static str = "<Insert>";
pub const DELETE: &'static str = "<Delete>";
pub const SYS_RQ: &'static str = "<SysRq>";
pub const CAPS_LOCK: &'static str = "<CapsLock>";
pub const SCROL_LOCK: &'static str = "<ScrollLock>";
pub const NUM_LOCK: &'static str = "<NumLock>";
pub const KP0: &'static str = "<KP0>";
pub const KP1: &'static str = "<KP1>";
pub const KP2: &'static str = "<KP2>";
pub const KP3: &'static str = "<KP3>";
pub const KP4: &'static str = "<KP4>";
pub const KP5: &'static str = "<KP5>";
pub const KP6: &'static str = "<KP6>";
pub const KP7: &'static str = "<KP7>";
pub const KP8: &'static str = "<KP8>";
pub const KP9: &'static str = "<KP9>";
pub const KP_STAR: &'static str = "<KP*>";
pub const KP_DIV: &'static str = "<KP/>";
pub const KP_ADD: &'static str = "<KP+>";
pub const KP_SUB: &'static str = "<KP->";
pub const KP_POINT: &'static str = "<KP.>";
pub const KP_ENTER: &'static str = "<KPEnter>";
}
File renamed without changes.
90 changes: 44 additions & 46 deletions src/system/linux/input.rs → src/system/unix/input.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -21,63 +22,60 @@ pub struct InputEvent {
pub value: i32
}

// Unknown key string
const UK: &'static str = "<UK>";

const KEY_NAMES: [&'static str; MAX_KEYS as usize] = [
UK, "<ESC>",
Key::UK, Key::ESC,
"1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "=",
"<Backspace>", "<Tab>",
Key::BACKSPACE, Key::TAB,
"q", "w", "e", "r", "t", "y", "u", "i", "o", "p",
"[", "]", "<Enter>", "<LCtrl>",
"[", "]", Key::ENTER, Key::LCTRL,
"a", "s", "d", "f", "g", "h", "j", "k", "l", ";",
"'", "`", "<LShift>",
"'", "`", Key::LSHIFT,
"\\", "z", "x", "c", "v", "b", "n", "m", ",", ".", "/",
"<RShift>",
"<KP*>",
"<LAlt>", " ", "<CapsLock>",
"<F1>", "<F2>", "<F3>", "<F4>", "<F5>", "<F6>", "<F7>", "<F8>", "<F9>", "<F10>",
"<NumLock>", "<ScrollLock>",
"<KP7>", "<KP8>", "<KP9>",
"<KP->",
"<KP4>", "<KP5>", "<KP6>",
"<KP+>",
"<KP1>", "<KP2>", "<KP3>", "<KP0>",
"<KP.>",
UK, UK, UK,
"<F11>", "<F12>",
UK, UK, UK, UK, UK, UK, UK,
"<KPEnter>", "<RCtrl>", "<KP/>", "<SysRq>", "<RAlt>", UK,
"<Home>", "<Up>", "<PageUp>", "<Left>", "<Right>", "<End>", "<Down>",
"<PageDown>", "<Insert>", "<Delete>"
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, "<ESC>",
Key::UK, Key::ESC,
"!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "+",
"<Backspace>", "<Tab>",
Key::BACKSPACE, Key::TAB,
"Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P",
"{", "}", "<Enter>", "<LCtrl>",
"{", "}", Key::ENTER, Key::LCTRL,
"A", "S", "D", "F", "G", "H", "J", "K", "L", ":",
"\"", "~", "<LShift>",
"\"", "~", Key::LSHIFT,
"|", "Z", "X", "C", "V", "B", "N", "M", "<", ">", "?",
"<RShift>",
"<KP*>",
"<LAlt>", " ", "<CapsLock>",
"<F1>", "<F2>", "<F3>", "<F4>", "<F5>", "<F6>", "<F7>", "<F8>", "<F9>", "<F10>",
"<NumLock>", "<ScrollLock>",
"<KP7>", "<KP8>", "<KP9>",
"<KP->",
"<KP4>", "<KP5>", "<KP6>",
"<KP+>",
"<KP1>", "<KP2>", "<KP3>", "<KP0>",
"<KP.>",
UK, UK, UK,
"<F11>", "<F12>",
UK, UK, UK, UK, UK, UK, UK,
"<KPEnter>", "<RCtrl>", "<KP/>", "<SysRq>", "<RAlt>", UK,
"<Home>", "<Up>", "<PageUp>", "<Left>", "<Right>", "<End>", "<Down>",
"<PageDown>", "<Insert>", "<Delete>"
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
Expand All @@ -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;
}
}

Expand Down

0 comments on commit b2d074e

Please sign in to comment.