65 lines
2.2 KiB
Rust
65 lines
2.2 KiB
Rust
use std::ffi::OsString;
|
|
|
|
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 {
|
|
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,
|
|
alt_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,
|
|
_ => {}
|
|
}
|
|
|
|
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
|
|
pub fn is_aux_key(&self) -> bool {
|
|
matches!(self.kbdllstruct.vkCode, 160 | 161 | 162 | 163 | 91)
|
|
}
|
|
|
|
#[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 | 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());
|
|
OsString::from_wide(&out[..null_pos]).to_str().unwrap().to_owned()
|
|
}
|
|
}
|
|
} |