move keystate into own file
configure clippy linter adjust style to linter settings
This commit is contained in:
parent
8948353106
commit
9e78b067f5
43
src/key/keystate.rs
Normal file
43
src/key/keystate.rs
Normal 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
1
src/key/mod.rs
Normal file
|
@ -0,0 +1 @@
|
||||||
|
pub mod keystate;
|
14
src/lib.rs
14
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 winapi::um::winuser::GetKeyNameTextW;
|
||||||
use std::ffi::OsString;
|
use std::ffi::OsString;
|
||||||
use wio::wide::FromWide;
|
use wio::wide::FromWide;
|
||||||
|
|
||||||
pub mod logging;
|
pub mod logging;
|
||||||
|
pub mod key;
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub fn scan_code_to_key_name(scan_code: u32) -> String {
|
pub fn scan_code_to_key_name(scan_code: u32) -> String {
|
||||||
unsafe {
|
unsafe {
|
||||||
let mut out = [0u16; 128];
|
let mut out = [0_u16; 128];
|
||||||
GetKeyNameTextW((scan_code << 16) as i32, (&mut out).as_mut_ptr(), 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());
|
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
|
//use to_string_lossy to avoid unicode checks for better performance. if the windows api screws up thats not my fault :P
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
use winapi::um::winuser::KBDLLHOOKSTRUCT;
|
|
||||||
|
|
||||||
use crate::logging::loggers::{ConsoleLogger, FileLogger};
|
use crate::logging::loggers::{ConsoleLogger, FileLogger};
|
||||||
use crate::scan_code_to_key_name;
|
use crate::scan_code_to_key_name;
|
||||||
|
use crate::key::keystate::KeyState;
|
||||||
|
|
||||||
pub trait Logger: Sync {
|
pub trait Logger: Sync {
|
||||||
fn log(&self, key: &KeyState);
|
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
use crate::logging::logger::{Logger, KeyState};
|
use crate::logging::logger::Logger;
|
||||||
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;
|
||||||
use std::sync::Mutex;
|
use std::sync::Mutex;
|
||||||
|
use crate::key::keystate::KeyState;
|
||||||
|
|
||||||
pub struct ConsoleLogger;
|
pub struct ConsoleLogger;
|
||||||
impl Logger for ConsoleLogger {
|
impl Logger for ConsoleLogger {
|
||||||
|
|
|
@ -7,7 +7,7 @@ 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, KeyState};
|
use jenslog_rs::{key::keystate::KeyState, logging::logger::{Logger, get_logger}};
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref KEYSTATE: Mutex<Box<KeyState>> = Mutex::new(KeyState::new());
|
static ref KEYSTATE: Mutex<Box<KeyState>> = Mutex::new(KeyState::new());
|
||||||
|
|
Loading…
Reference in a new issue