fix key names once and for all!

hopefully...
This commit is contained in:
LordMZTE 2020-08-26 15:18:43 +02:00
parent 8bba874cd6
commit b953466376
2 changed files with 13 additions and 4 deletions

View file

@ -1,14 +1,16 @@
use std::ffi::OsString;
use winapi::um::winuser::{GetKeyNameTextW, KBDLLHOOKSTRUCT};
use winapi::um::winuser::{GetKeyNameTextW, KBDLLHOOKSTRUCT, LLKHF_ALTDOWN, LLKHF_EXTENDED};
use wio::wide::FromWide;
#[allow(clippy::struct_excessive_bools)]
pub struct KeyState {
pub kbdllstruct: KBDLLHOOKSTRUCT,
pub shift_down: bool,
pub ctrl_down: bool,
pub win_down: bool,
pub alt_down: bool,
}
impl KeyState {
@ -24,6 +26,7 @@ impl KeyState {
shift_down: false,
ctrl_down: false,
win_down: false,
alt_down: false,
})
}
@ -37,6 +40,10 @@ impl KeyState {
91 => self.win_down = key_down,
_ => {}
}
if key.flags & LLKHF_ALTDOWN != 0 {
self.alt_down = key_down
}
}
/// true if the key is an auxiliary key like shift, control or the windows key
@ -47,10 +54,11 @@ impl KeyState {
#[no_mangle]
pub fn name(&self) -> String {
unsafe {
#[allow(clippy::if_not_else)] //clippy wrongfully assumes the != here is unecessary
let flags = if self.kbdllstruct.flags & LLKHF_EXTENDED != 0 { 1 << 24 } else { 0 };
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);
GetKeyNameTextW((self.kbdllstruct.scanCode << 16 | flags /*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

@ -15,11 +15,12 @@ pub fn get_logger(name: &str) -> Box<dyn Logger> {
impl dyn Logger {
pub fn default_format(key: &KeyState) -> String {
format!(
"{} >> {}{}{} >> {}",
"{} >> {}{}{}{} >> {}",
key.kbdllstruct.vkCode,
if key.win_down { "w" } else { "" },
if key.ctrl_down { "c" } else { "" },
if key.shift_down { "s" } else { "" },
if key.alt_down { "a" } else { "" },
key.name()
)
}