move keystate into own file

configure clippy linter
adjust style to linter settings
This commit is contained in:
LordMZTE 2020-08-26 13:51:43 +02:00
parent 8948353106
commit 9e78b067f5
6 changed files with 61 additions and 47 deletions

43
src/key/keystate.rs Normal file
View file

@ -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<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)
}
}

1
src/key/mod.rs Normal file
View file

@ -0,0 +1 @@
pub mod keystate;

View file

@ -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

View file

@ -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<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,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 {

View file

@ -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<Box<KeyState>> = Mutex::new(KeyState::new());